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 Comments

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);
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.
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?
my problem is to be able to read even with a loop for a series of files "s" string. I can't do that.
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
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?
Are all of the files in the same folder (see dir)? Different folders? Do you want the user to choose the file (see uigetfile)?
The files are in the same folder and I want to consider all the files and not choose individual files.

Sign in to comment.

 Accepted Answer

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 Comments

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??
What is your current code?
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.
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.
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);
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
thanks!!
"Accept this answer at the top of the answer to actually accept it." It does not appers this possibility

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 10 Apr 2020

Commented:

on 20 Apr 2020

Community Treasure Hunt

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

Start Hunting!