Open multiple file as string

Hi guys,
I have a question.
I have different file like string, and I want to open it togheter, without write the name
I tryd this code, but in this way I have to write the name of single file.
Have you advice about it?
directory='D:\Documents\numeric\pip\';
fittingresultspath = 'D:\Documents\numeric\fitting_results\pip\';
fid = -1; msg = ''; while fid < 0
disp(msg); filename = input('input: ','s'); fid=1;
end
Thank you so much

8 comentarios

Geoff Hayes
Geoff Hayes el 10 de Abr. de 2020
FC - do you want to keep prompting the user for a file (to read) until the file can be opened?
msg = '';
while fid < 0
disp(msg);
filename = input('input: ','s');
fid=fopen(filename, 'r');
end
% do something with the file
% close the file
fclose(fid);
Fredic
Fredic el 11 de Abr. de 2020
Thank you so much for your answer.
I have a series of files like string and I have to open them to run them in my code.
How can I open them all together?
I don't want to call them one by one, but given the folder I want to open them automatically, being so many files.
Geoff Hayes
Geoff Hayes el 11 de Abr. de 2020
FC - you'll need to open the files one by one possibly with a loop. I don't understand why you'd want to open them all at once. Also, what do you mean by series of files like string?
Fredic
Fredic el 11 de Abr. de 2020
my problem is to be able to read even with a loop for a series of files "s" string. I can't do that.
Geoff Hayes
Geoff Hayes el 11 de Abr. de 2020
I'm not really sure what you mean by for a series of files "s" string. What happens when you try something like
while true
filename = input('please input a file name: ','s');
fid=fopen(filename, 'r');
if fid > 0
% do something with file
% close file
fclose(fid);
else
fprintf('Could not open file %s\n', filename);
end
response = input('Are you finished (Y/N): ','s');
if strcmpi(response,'Y')
break;
end
end
Fredic
Fredic el 11 de Abr. de 2020
It is appeared in the command windows:
----
please input a file name: Ra25721_minor_op_
Could not open file Ra25721_minor_op_
Are you finished (Y/N):
----
appeared my results of analysis.
If I want consider the files automatically, without that I have to write the name?
Geoff Hayes
Geoff Hayes el 11 de Abr. de 2020
Are all of the files in the same folder (see dir)? Different folders? Do you want the user to choose the file (see uigetfile)?
Fredic
Fredic el 12 de Abr. de 2020
The files are in the same folder and I want to consider all the files and not choose individual files.

Iniciar sesión para comentar.

 Respuesta aceptada

Geoff Hayes
Geoff Hayes el 13 de Abr. de 2020
FC - try the following
folderName = '/Users/somename/somepath'; % example folder that has files we are interested in
filesInFolder= dir(folderName);
for k = 1:length(filesInFolder)
fprintf('file name: %s\n',filesInFolder(k).name);
% do something with this file
end

8 comentarios

Fredic
Fredic el 20 de Abr. de 2020
thank you so much for your answer.
Now appear this message:
Error using horzcat
The following error occurred converting from char to struct: Conversion to struct from char is not possible.
Do you have any advice??
Walter Roberson
Walter Roberson el 20 de Abr. de 2020
What is your current code?
Fredic
Fredic el 20 de Abr. de 2020
Editada: Geoff Hayes el 20 de Abr. de 2020
I have different file like string, and I want to open it togheter, without write the name
I tryd this code, but in this way I have to write the name of single file.
directory='D:\Documents\numeric\pip\';
fittingresultspath = 'D:\Documents\numeric\fitting_results\pip\';
fid = -1;
msg = '';
while fid < 0
disp(msg);
filename = input('input: ','s');
fid=1;
end
it is working very well, but in this way I have to write each single file to run my code.
Geoff Hayes
Geoff Hayes el 20 de Abr. de 2020
FC - but the above code doesn't throw the horzcat error...and it looks like the code that you have originally posted. Please show the code that you are trying to use which is causing the error.
Fredic
Fredic el 20 de Abr. de 2020
directory='D:\Documents\numeric\pip\';
fittingresultspath = 'D:\Documents\numeric\fitting_results\pip\';
fid = -1; msg = ''; while fid < 0
disp(msg); filename = input('input: ','s'); fid=1;
end
%%
angle = directory;
%% ------------------------------------------------------------------------
while reinisch_in_plane + gasser_2006 + reinisch_out_of_plane ~= 1
fprintf('ERROR: choose ONE model for fitting!')
break
end
% read and adjust data
rawdata = dlmread([angle,filename]);
size_rawdata = size(rawdata);
Geoff Hayes
Geoff Hayes el 20 de Abr. de 2020
The while loop is unnecessary since you don't actually try to open the file in the body of the loop. This code can be replaced with just
filename = input('input: ','s');
The msg and fid are not used (or don't add anything to the code). I'm assuming that concatenation error is coming from
[angle,filename]
but this seems to work okay for me (though I would recommend using fullfile to build the full file name and path from the two inputs. Also, the variable angle seems incorrectly named for the purpose here.
I think that you need to use the MATLAB debugger to see what angle is and what filename is. Perhaps one or both is being overwritten in your while loop (I'm guessing that you've omitted some code)
while reinisch_in_plane + gasser_2006 + reinisch_out_of_plane ~= 1
fprintf('ERROR: choose ONE model for fitting!')
break
end
Fredic
Fredic el 20 de Abr. de 2020
thanks!!
Fredic
Fredic el 20 de Abr. de 2020
"Accept this answer at the top of the answer to actually accept it." It does not appers this possibility

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Debugging and Analysis en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 10 de Abr. de 2020

Comentada:

el 20 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by