How to get matlab to read the code
2 次查看(过去 30 天)
显示 更早的评论
I have converted data from OnScale to matlab. I have been given a script which will read the data from this subfolder. The data loads in fine but I keep getting this error messages,
Error using load
Unable to read file 'C:\Users\User\Dropbox\My PC (LAPTOP-8G4JCC0U)\Documents\onscale1\2d_simple_array.mat'. No
such file or directory.
Error in example_import (line 15)
load(strcat('C:\Users\User\Dropbox\My PC
(LAPTOP-8G4JCC0U)\Documents\onscale',num2str(i),'\2d_simple_array.mat'));
All the files are in the same directory and folder as far as I can see, so maybe something needs changed in the code I have attahced screen shots of my code from line 15.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/536851/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/536856/image.png)
Thanks
0 个评论
采纳的回答
Walter Roberson
2021-3-3
Your code has
load(strcat('C:\Users\User\Dropbox\My PC (LAPTOP-8G4JCC0U)\Documents\onscale',num2str(i),'\2d_simple_array.mat'));
^^^^^^^^^^^
That num2str(i) is inserting a 1 onto the end of the directory name, giving you a directory name of onscale1 but your files are in a directory named onscale with no 1
3 个评论
Image Analyst
2021-3-3
You should do something more robust inside the loop like this:
parentFolder = 'C:\Users\User\Dropbox\My PC (LAPTOP-8G4JCC0U)\Documents\onscale';
% Construct this particular subfolder name with a number on the end of it.
thisFolder = fprintf('%s/onscale%d', parentFolder, i);
% Check to see if the folder exists. Warn user if it does not exist.
if ~isfolder(thisFolder)
warningMessage = sprintf('WARNING: folder does not exist:\n%s', thisFolder)
uiwait(warndlg(warningMessage));
continue; % Skip this one - can't do anything.
end
% If you get here the folder exists.
% Construct this particular file name.
fullFileName = fullfile(thisFolder, '\2d_simple_array.mat');
% Check to see if the MAT file exists. Warn user if it does not exist.
if ~isfile(fullFileName)
warningMessage = sprintf('WARNING: MAT file does not exist:\n%s', fullFileName)
uiwait(warndlg(warningMessage));
continue; % Skip this one - can't do anything.
end
% If you get here both the folder and the file exist.
load(fullFileName);
Notice there are lots of validation checks and warnings to alert you if the folder or file is missing, yet it keeps going with the rest of the files if it encounters a missing file. You could also replace uiwait(warndlg()) with questdlg() if you want to give them the opportunity to bail out completely and NOT process the rest of the files if you encounter a missing file. Or you could replace the uiwait(warndlg()) with fprintf() if you just want to print to the command window and not stop to alert the user at all.
Walter Roberson
2021-3-3
I don't really understand why that changes the directory by adding a 1 onto the end
Because that is what strcat does
i = 7;
strcat('Lucky number=', num2str(i))
You have the strcat() operation at the point that you are constructing the directory name, not at the point where you are constructing the file name.
This code was given to me
Rename your directory from
C:\Users\User\Dropbox\My PC (LAPTOP-8G4JCC0U)\Documents\onscale
to
C:\Users\User\Dropbox\My PC (LAPTOP-8G4JCC0U)\Documents\onscale1
and the code will work for the first iteration.
The code as written also expects directories onscale2, onscale3, onscale4, up to onscale16, each with a file 2d_simple_array.mat in it.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Search Path 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!