Error message while using Fullfile to write .m file in a specific folder

Hello,
I have some troubles using fullfile function in order to store outputs in a specific folder.
Error using fullfile (line 61) An unknown error occurred in FULLFILE while constructing the file specification.
Error in Test_Export (line 14) f=fullfile(myFolder,fileID);
Caused by: Error using horzcat The following error occurred converting from logical to char: Error using char Conversion to char from logical is not possible.
The code i'm using is attached. Any ideas please to fix it?
Thank you in advance for your help
Fatzo

 Akzeptierte Antwort

There are two problems with your code:
myFolder=mkdir('C:\Users\fatima.smime\Documents\MATLAB\Pro...')
myFolder is not the name of the folder you've created, it's just the status code telling you whether or not the folder creation was succesful. This is possibly what you meant:
myfolder = fullfile('C:\Users\fatima.smime\Documents\MATLAB\Programmes These FZS\Plaque\Modele direct plaque\Avril 2015\Simulations_MD', filename);
mkdir(myfolder);
Secondly, as pointed out by pfb,
f = fullfile(myFolder, 'fileID');
should be
f = fullfile(myFolder, fileID);
While the above two should stop the error from happening, your code is very confusing. Is filename the name of a file or a folder? It sounds like the name of a file, yet you use it to create a folder. Secondly, why do you have \n in some of your num2str conversion. Thankfully they're ignored as that would cause problem later in your code.
Finally, I find sprintf much easier to read than num2str + string concatenation. It's also more efficient:
filename= sprintf('pas%dfreq%gFech%gNtot%d', pas, f0, 1/Tech, Ntot1);

8 Kommentare

pfb
pfb am 28 Apr. 2015
Bearbeitet: pfb am 28 Apr. 2015
yes, that's it! There is the source of the "logical" the error is talking about.
myFolder=mkdir('C:\Users\fatima.smime\Documents\MATLAB\Pro...');
Well spotted!
I also agree with the advice of using sprintf instead of num2str.
Great! Thx for your answers and advices.
In fact filename is the name of the folder in which i save fileID=['tilt%+fprof%f','.m']. Where fileID contains the workspace matrices (MTi,MT2,MT3,STOF).
By the way, do i have to specify the directory if i want to load and process data from fileID?
Using a variable called filename to store a folder is misleading. You'd be better off calling it foldername (and I'd rename fileID to filename).
You can load a file without specifying its full path if it's in the current directory, which you change with cd. However, your code would be more robust (less chance of bugs in the future) if you do specify the path.
F Z
F Z am 28 Apr. 2015
Bearbeitet: F Z am 28 Apr. 2015
Thank you for these advices.
I specified the path with cd ('C:\Users\fatima.smime\Documents\MATLAB\Programmes These FZS\Plaque\Modele direct plaque\Avril 2015\Simulations_MD\')
But, once the path is specified, what is the command to access the foldername named [foldername= sprintf('pas%dfreq%gFech%gNtot%d', pas, f0, 1/Tech, Ntot1)] then load the mat file named [filename=sprintf('tilt%dprof%g',tiltd,prof)] in that folder
Assuming that foldername is in the 'C:\Users\fatima.smime\Documents\MATLAB\Programmes These FZS\Plaque\Modele direct plaque\Avril 2015\Simulations_MD\' directory, then
load(fullfile(foldername, filename))
should work.
otherwise:
basefolder = 'C:\Users\fatima.smime\Documents\MATLAB\Programmes These FZS\Plaque\Modele direct plaque\Avril 2015\Simulations_MD\';
load(fullfile(basefolder, foldername, filename));
will work without any need for cd.
Actually both load(fullfile(foldername, filename)) and load(fullile(basefolder, foldername, filename))
Returns the same error message: Undefined function 'fullile' for input arguments of type 'char'.
Error in Test_Import (line 11) m=load(fullile(basefolder, foldername));
Undefined function 'fullile' for input arguments of type 'char'.
Error in Test_Import (line 11) m=load(fullile(basefolder, foldername,filename));
As basefolder, foldername and filename are defined as char in the Workspace
It should have been obvious that fullile was a typo and that it should have read fullfile. This is after all the title of your question.
I've now fixed the typo.
99% of the errors of the type undefined function xxx for input argument of type yyy are due to the function xxx not being found either because it is mispelt or because it's not on the path. It's rarely to do with the arguments.
Really sorry. In fact, i realize that it was a silly question. Should've checked the spelling before asking.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

pfb
pfb am 28 Apr. 2015
I think it might have to do with this: in your code you define the name of the file in a char variable
fileID=['tilt',num2str(tiltd,'%-f'),'prof',num2str(depth_d),'.m'];
which looks something like 'tiltFFFFfprofDDDD.m'
but then, when you call fullfile, you do not use that char variable (fileID), but 'fileID'
f=fullfile(myFolder,'fileID');
I think you meant
f=fullfile(myFolder,fileID);

2 Kommentare

1st, thank you for your reply
Actually, both f=fullfile(myFolder,fileID); and f=fullfile(myFolder,'fileID');
returns the same error message
ok... can you post a mat file containing the variables "myFolder" and "fileID" you feed to fullfile?
The path in your code is for a Windows system. Apologies for the silly question, but are you running your code on a Windows machine?
Finally, formatting the code bits in your posts would make them more readable.

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

F Z
am 28 Apr. 2015

Kommentiert:

F Z
am 29 Apr. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by