1

我的 matlab 脚本中有 2 个函数。这些函数通过*.mat文件交换数据。在其中一个函数中,我读取了特定文件的数量和其中的数据。后来我处理并将变量存储ggdproj1.mat文件中。如下所示 :

    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

第二个功能是:

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

有趣的是,当我在 Matlab 工具中运行这两个函数时,它们工作得非常好。后来,我为我的应用程序编译了独立的可执行文件。这个独立的应用程序在第一个功能之前可以正常工作;但是,当我调用第二个函数时,它会proj1.mat从另一个包含完全不同数据的未知位置加载文件。
不知道导致这种奇怪行为的问题是什么。

我在独立执行时检查了第二个函数中的当前路径(pwd),发现它与第一个函数中的路径相同。

有任何想法吗 ??

4

1 回答 1

1

如果您需要在已部署的应用程序中继续写入和读取文件,我建议在用户机器的 App Data 中创建一个本地目录并将所有文件存储在那里。您可以使用matlab 中心的以下函数来执行此操作。例如像

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

应该在用户的计算机上创建一个目录(如果它不存在),它将存储所有文件,并且变量root将为您提供该目录的绝对路径。然后,您可以使用它来保存和加载文件。例如

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

在第一个函数中,并且

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

在第二个功能中。这样您就可以准确地知道文件的存储位置。另外,请阅读我之前在评论中提到的链接。

于 2017-06-08T21:56:19.697 回答