Opening a file that contains a user defined word

조회 수: 1 (최근 30일)
D F
D F 2017년 11월 3일
댓글: D F 2017년 11월 6일
I have a code that asks the user to input 4 numbers. I then need to open the txt file that contains these numbers. The txt file's name is: 'PlotValues(then the user defined numbers).txt. The code correctly gets the user defined numbers (called Numbers below), it is from then where it breaks:-
Numbers = answer
FileName = 'PlotValues'Numbers'.txt';
file_id = fopen(FileName);
data = textscan(file_id,'%f %f','HeaderLines', 1);
fclose(file_id);
x = data{1};
y = data{2};
plot(x,y)

채택된 답변

Chenchal
Chenchal 2017년 11월 3일
% code
% Issue may be with how you are using Numbers
% assumption: Numbers is a vector
% convert Numbers to row vector and do a num2str before concat
fileBase = 'PlotValues';
fileExt = '.txt';
Numbers = [1;2;3;4]; % or [1 2 3 4]
FileName = [fileBase num2str(Numbers(:)','%d') fileExt];
% Check if filename matches the file on disk
% see for textscan
<https://www.mathworks.com/help/matlab/ref/textscan.html textscan>
  댓글 수: 1
D F
D F 2017년 11월 6일
Brilliant! Thanks for the help, Numbers was a cell array so cell2mat has worked

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by