After convert gui to exe, creat a new file in a certain path does not work in other computers with exe installed, but locally works. Is the problem because of 'fopen' part ?

1 view (last 30 days)
if true
function save_button_Callback(hObject, eventdata, handles)
% hObject handle to save_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = guidata(handles.output);
name = handles.dataname;
%parameters = [''Date: ''; 'Exposure Time: '; 'Voltage: '; 'Correction Method: '; 'Window Size: '; 'Step Size: '];
data = num2cell([handles.RS handles.BC]);
items = cellstr(char('Date: ', 'Exposure Time: ', 'Voltage: ', 'xmin: ', 'xmax: ', 'Method: ', 'Window Size: ', 'Step Size: '));
parameters = cellstr(char(handles.date, handles.exposure, handles.voltage, num2str(handles.xminDef), num2str(handles.xmaxDef), ...
handles.correct_method, num2str(handles.winsize), num2str(handles.stepsize)));
save_data = cell(size(data,1), 4);
save_data(:, 1:2) = data;
save_data(1:length(items), 3) = items;
save_data(1:length(parameters), 4) = parameters;
save_path = get(handles.edit11, 'String');
if(isempty(save_path) == 1)
str = get(handles.edit1, 'String');
mkdir(str, 'BaselineCorrected');
save_path = strcat(str, '/BaselineCorrected');
set(handles.edit11, 'String', save_path);
end
handles.savepath = get(handles.edit11, 'String');
file = fopen(fullfile(handles.savepath, [name(1:end-4), '_BaselineCorrected.dat']), 'wt+');
formatSpec = '%15.2f %15.4f %15s %15s\r\n';
[nrows,ncols] = size(save_data);
for i = 1:nrows
fprintf(file, formatSpec, save_data{i,:});
end
fclose(file);
guidata(hObject,handles);
end
  4 Comments
Jan
Jan on 12 Feb 2016
Edited: Jan on 12 Feb 2016
"It doesn't work" is too lean to reconsider the problem. Please explain what you observe.
Note that the success of fopen should always be checked:
fid = fopen(...)
if fid == -1, error('A meaningful error message here: ...'); end
Yuxiang Gong
Yuxiang Gong on 12 Feb 2016
As shown in the figure, the edit11 shows the path normally and the path exists. When I click the 'SAVE' button, it simply returns a error sound and nothing happened. This UI in .exe format in my computer works properly, but seems not portable.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 11 Feb 2016
handles.edit11 might be non-empty but with the information given we do not know that it is valid. If it is empty then handles.edit1 will be used to create a directory and path, but we do not know that handles.edit1 is valid.
Your mkdir() should have a try/catch around it in case creating the directory fails.
Your fopen() should have two outputs and you should be checking if the first output is negative and displaying the second output (error message) if so.
  2 Comments
Yuxiang Gong
Yuxiang Gong on 12 Feb 2016
Edited: Yuxiang Gong on 12 Feb 2016
Both handles.edit11 and handles.edit1 are valid. I'm able to creat and save files in my computer, however, when I install the .exe file in other computers, this step dose not work.
I think that the writing permission could be the problem, but I'm not sure.

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!