MATLAB Answers

How to Ignore the last line from a file read

2 ビュー (過去 30 日間)
Antonio Carvalho
Antonio Carvalho 2019 年 12 月 6 日
Edited: Image Analyst 2019 年 12 月 7 日
Hello people,
I have the folowing file and I would like to plot it with Matlab. As you can see, the last two line are not to be print, are letters. So, how can I ignore it ? That is, I want to read and plot only the four first lines. Anyone can help with it.
1 3 10
2 6 20
3 9 30
4 12 40
END ENDe ENDe
END ENDe ENDe

  0 件のコメント

サインイン to comment.

採用された回答

Image Analyst
Image Analyst 2019 年 12 月 7 日
編集済み: Image Analyst 2019 年 12 月 7 日
Try this:
% Read in data.
d = importdata('data.txt')
% Now plot each row of data
for k = 1 : 4
plot(d(k, :), '.-', 'LineWidth', 2, 'MarkerSize', 30);
hold on;
legendStrings{k} = sprintf('Row #%d', k);
end
grid on
legend(legendStrings, 'Location', 'north');
Or, with the new data file you attached, perhaps this:
% Read in data.
d = readmatrix('mech_flu.txt', 'Range','A5:c4849')
% Extract just the first 4 rows, like he asked for.
x = d(1:4, 1);
y1 = d(1:4, 2);
y2 = d(1:4, 3);
% Now plot each column of data, but just the first 4 rows
plot(x, y1, '.-', 'LineWidth', 2, 'MarkerSize', 30);
hold on;
plot(x, y2, '.-', 'LineWidth', 2, 'MarkerSize', 30);
grid on
legend('y1', 'y2', 'Location', 'east');
0001 Screenshot.png

  0 件のコメント

サインイン to comment.

More Answers (1)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019 年 12 月 6 日
s=readmatrix(filename);
s(any(isnan(s),2),:)=[];
figure
plot3(s(:,1),s(:,2),s(:,3),'*-r')

  4 件のコメント

表示 1 件の古いコメント
JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019 年 12 月 6 日
Please upload your file here
Walter Roberson
Walter Roberson 2019 年 12 月 7 日
textscan() the file with 'headerlines', 4 and a format of '%f%f%f' . It will automatically stop reading when it encounters the text because the text does not match the numeric format.
Walter Roberson
Walter Roberson 2019 年 12 月 7 日
first_line = 1000;
last_line = 2000;
fmt = '%f%f%f';
fid = fopen('mech_flu.txt');
data = cell2mat( textscan(fid, fmt, last_line - first_line + 1, 'HeaderLines', first_line - 1, 'CollectOutput', true));
fclose(fid)

サインイン to comment.

サインイン してこの質問に回答します。


Translated by