Info

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

strange behavior in selecting/loading file

1 view (last 30 days)
fk
fk on 4 Apr 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
At the beginning of a script, I bring up a dialog that allows me to select a directory (=group), and one of the *.mat files contained in it. Name and path of the selected file are returned in the variables fname and pname.
Here's a very odd behavior: when I run the script, the file does not get read in, and fname or pname = 0; it's as if the code jumped over line 4. When I go into debugging mode and step through that part of the code, there is no problem: it brings me to the selected directory and lets me chose a file which is then processed further; pname and fname get filled correctly, and assembled as correct file path & name in pp2. What gives?
k = menu('Choose a group','G01','G02','G03','G04');
what2 = ['G01';'G02';'G03';'G04'];
group = what2(k,:);
[fname, pname] = uigetfile(['C:\Data\exp1\' group '\post_step1\????r.mat']);
pp2=[pname fname];
eval(['load ' pp2 ';']);

Answers (1)

Image Analyst
Image Analyst on 4 Apr 2015
Try this:
button = menu('Choose a group','G01','G02','G03','G04')
what2 = ['G01';'G02';'G03';'G04'];
group = what2(button,:);
filePattern = sprintf('C:/Data/exp1/%s/post_step1/????r.mat', group)
[baseFileName, folder] = uigetfile(filePattern)
if baseFileName ~= 0
fullFileName = fullfile(folder, baseFileName)
storedStructure = load(fullFileName)
end
Note: use forward slashes in sprintf() - forward slashes work just fine in Windows and Linux.
  2 Comments
fk
fk on 4 Apr 2015
Edited: fk on 4 Apr 2015
Thank you! Interestingly, the behavior is very similar: when I run the script, it returns the correct filePattern, but baseFileName and folder = 0.
When I step through, it works fine, and returns correct filePattern and baseFileName, and lets me chose a file. However, there's another snag shortly afterwards: when calling a variable (which does show up in storedStucture as b1: [194x1 double]), it returns an error
Undefined function 'b1' for input arguments of type 'double'.
Do I need to do something special to make the variables in the struct accessible? (Previously all variables showed up separately in the workspace).
Here's one other bit which might help to clarify the issue: when running the old version of the script, I now found that if I repeat line 4 (running it twice in a row):
[fname, pname] = uigetfile(['C:\Data\exp1\' group '\post_step1\????r.mat']);
it calls the correct folder and file, and works...
Image Analyst
Image Analyst on 5 Apr 2015
It only returns 0 if you click Cancel.

Community Treasure Hunt

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

Start Hunting!