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

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 2020년 4월 11일
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 2020년 4월 11일
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 2020년 4월 11일
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
Fredic
Fredic 2020년 4월 11일
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 2020년 4월 11일
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 2020년 4월 12일
The files are in the same folder and I want to consider all the files and not choose individual files.

댓글을 달려면 로그인하십시오.

 채택된 답변

Geoff Hayes
Geoff Hayes 2020년 4월 13일

1 개 추천

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

Fredic
Fredic 2020년 4월 20일
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 2020년 4월 20일
What is your current code?
Fredic
Fredic 2020년 4월 20일
편집: Geoff Hayes 2020년 4월 20일
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 2020년 4월 20일
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
Fredic
Fredic 2020년 4월 20일
thanks!!
Fredic
Fredic 2020년 4월 20일
"Accept this answer at the top of the answer to actually accept it." It does not appers this possibility

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

태그

질문:

2020년 4월 10일

댓글:

2020년 4월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by