adi
adi

Reputation: 39

Loading '*.mat' correctly but form another unknown location

I got 2 functions in my matlab script. These functions exchange the data via a *.mat file. In one of the functions, I read the number of specific files and a data from them. Later I process and store the variable ggd in proj1.mat file. As shown below :

    function browse_pushBtn_Callback(hObject, eventdata, handles)
% hObject    handle to browse_pushBtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.directory = uigetdir('C:\temp\');
guidata(hObject,handles);
ggd.directory = handles.directory;
if (ggd.directory)
    set(handles.browse_textEdit,'String',handles.directory);
    disp2listbox(handles.mainLog_listBox, '1. Searching for GGD Datafile:');
    ggd = MyRead(ggd);
    disp2listbox(handles.mainLog_listBox, sprintf('     -%d Bilder 
    gefunden',length(ggd.bilds)));
    save proj1 ggd;
else
    set(handles.browse_textEdit,'String','Select a directory');
end

The Second function is :

function start_pushBtn_Callback(hObject, eventdata, handles)
% hObject    handle to start_pushBtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

option = get(handles.mainMenu,'Value');
if (option == 1)
    disp2listbox(handles.mainLog_listBox, '2. Calculating the quadratic size..');
    fprintf('path is : %s\n', pwd); % did just to check the path (for debugging purpose)
    load proj1 ggd;
    ggd = MyProcess(ggd); 
    save proj1 ggd;
    disp2listbox(handles.mainLog_listBox, sprintf('     -MySize: %d X %d',ggd.X(1), ggd.Y(2)));
    disp2listbox(handles.mainLog_listBox, '3. opening another function...');
    % call to another function 
elseif (option == 2)
    disp2listbox(handles.mainLog_listBox, '2. opening third function...');
    % call to third different function
end

The funny thing is that these two functions work perfectly fine when I run them in Matlab tool. Later, I compile the standalone executable for my application. This standalone application works fine until the first function; but, when I call the second function, it loads the proj1.mat file from another unknown location that contains totally different data.
Have no idea what is the problem that causing such weird behaviour.

I checked the current paths (pwd) in second function while executing standalone and found it to be the same as the one in first function.

Any ideas ??

Upvotes: 1

Views: 56

Answers (1)

Vahe Tshitoyan
Vahe Tshitoyan

Reputation: 1439

If you need to keep writing and reading from a file in a deployed application, I would recommend creating a local directory in user's machine's App Data and storing all the files there. You can do this by using the following function from matlab central. E.g. something like

file_root = getapplicationdatadir('your.application.name',1,1);

should create a directory (if it does not exist) on the user's computer where it will store all the files, and variable root will give you the absolute path to that directory. You can then use it to save and load the files. E.g.

save([file_root '/proj1.mat'],'ggd');

in the first function, and

load([file_root '/proj1.mat'],'ggd');

in the second function. This way you know exactly where the file is stored. Also, read the link I mentioned earlier in the comment.

Upvotes: 1

Related Questions