How to get matlab to read the code
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
David Harra
le 3 Mar 2021
Commenté : Walter Roberson
le 3 Mar 2021
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.
Thanks
0 commentaires
Réponse acceptée
Walter Roberson
le 3 Mar 2021
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 commentaires
Image Analyst
le 3 Mar 2021
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
le 3 Mar 2021
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Environment and Settings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!