Info

This question is closed. Reopen it to edit or answer.

When I compile the GUI, a problem occured

1 view (last 30 days)
Huseyin
Huseyin on 28 Mar 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi, I've just finished my GUI and tried to compile it as an .exe file (by deploytool). The program works but one part of the program gives an error. The suprising thing that, in GUI it works well but in .exe it doesn't. Here is the code, maybe you can help me;
try
nmat3=handles.nmat;
akorlar=chordlist5(nmat3,chord_chan_no);
[n,m]=size(akorlar);
a=0;
for k=1:n
liste(1+a,:)=findchordyazi(akorlar(k,1),akorlar(k,2),akorlar(k,3),akorlar(k,4));
a=a+1;
end
fid=fopen('Akorlar.txt','w');
for i=1:length(liste)
fprintf(fid,'%s\r\n',liste(i,:));
end
fclose(fid);
system('Akorlar.txt')
catch
helpdlg('Bu midide akor bulunmuyor veya yanlış bir kanal değeri seçtiniz','İşlem Sonucu')
end
As you understood the code, simply it is for listing some words in a text file. It always gives me the warning message on the helpdlg line. Whereas it shouldn't be happened.
Thanks

Answers (2)

Image Analyst
Image Analyst on 28 Mar 2014
This is not going to work:
fid=fopen('Akorlar.txt','w');
unless you are absolutely sure what the current folder is. I strongly recommend you not just assume everything is going to be in the current folder, because it's not. The current folder is not where your exe is. Your exe is not even there - it's actually in some hidden secret folder. Put a "pwd" in your code if you want to know where it is. You'll be surprised. To fix, make sure you use fullfile() to specify the full filename.
if ~exist(fullFileName , 'file')
message = sprintf('Error: file does not exist:\n%s', fullFileName);
uiwait(warndlg(message));
return;
end
fullFileName = fullfile('c:\whatever', 'Akorlar.txt');
fid=fopen(fullFileName ,'w');
if fid ~= -1
% Process the file......
Use a try catch like this that gives more info:
try
% Some code that might generate an error.
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end

Huseyin
Huseyin on 29 Mar 2014
Edited: Huseyin on 29 Mar 2014
I edited the code like that. Now it creates the .txt file to 'c:\' which is common in all computers. It works successfully in matlab gui but when I compile it as an .exe file, it doesnt work and gives me the warning message on the helpdlg line again. What did I do wrong?
fullFileName = fullfile('c:\', 'Akorlar.txt');
fid=fopen(fullFileName ,'w');
for i=1:length(liste)
fprintf(fid,'%s\r\n',liste(i,:));
end
fclose(fid);
system(fullfile(fullFileName));
catch
helpdlg('Something goes wrong, try again','Result')
end
  3 Comments
Huseyin
Huseyin on 29 Mar 2014
No, if I described myself like you said I am sorry for that, I didn't mean like that. I used 'pwd' and found the folder, which was 'matlab/bin'. Then I thought that every computer may not have that folder and this is the reason that it didn't work when I run the '.exe'. After that I changed the current folder as 'c:\' but it didn't work. I understood something wrong. But thanks anyway, I am searching for more solutions.
Image Analyst
Image Analyst on 29 Mar 2014
The point is, we don't know why your code went into the catch statement. And once it's there, it gives virtually no useful information, unlike my catch code which gives you the full error message and line of code that it failed on. Perhaps you should just use the old, brute force approach of printing stuff between every line of code to figure out the offending line. Before every line of code, put
fprintf('About to call fullfile...\n');
fullFileName = fullfile('c:\', 'Akorlar.txt');
fprintf('About to call fopen...\n');
fid=fopen(fullFileName ,'w');
fprintf('About to start the for loop...\n');
for i=1:length(liste)
and so on until you find out what line it is.

This question is closed.

Community Treasure Hunt

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

Start Hunting!