how can i read data given from a txt file and plot graph

5 ビュー (過去 30 日間)
seelan kumar
seelan kumar 2019 年 5 月 16 日
コメント済み: Erivelton Gualter 2019 年 5 月 17 日
How to read the data and plot graph from the data given.

回答 (2 件)

Erivelton Gualter
Erivelton Gualter 2019 年 5 月 16 日
編集済み: Erivelton Gualter 2019 年 5 月 16 日
You may check this other anser related to import data:
https://www.mathworks.com/matlabcentral/answers/460098-import-text-data-as-a-numeric-matrix-with-specific-delimiters
Also, I am attaching a code you might use:
%% Initialize variables.
filename = 'sound.txt';
delimiter = '\t';
startRow = 2;
%% Format for each line of text:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%*s%f%f%f%f%f%f%f%f%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to the format.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
%% Close the text file.
fclose(fileID);
%% Create output variable
sound = [dataArray{1:end-1}];
%% Clear temporary variables
clearvars filename delimiter startRow formatSpec fileID dataArray ans;
[X,Y] = meshgrid(-10:5:25, -15:5:100);
surf(X,Y,sound)
xlabel('X');
ylabel('Y');
zlabel('Sound');
  2 件のコメント
seelan kumar
seelan kumar 2019 年 5 月 17 日
is there any other simpler coding that you can send me??Because i feel like ur coding is too long and its not in my syllabus.Tq
Erivelton Gualter
Erivelton Gualter 2019 年 5 月 17 日
We can simpligy this as in the following.
startRow = 2;
fileID = fopen('sound.txt','r');
dataArray = textscan(fileID, '%*s%f%f%f%f%f%f%f%f%[^\n\r]', 'Delimiter', '\t', 'TextType', 'string', 'HeaderLines' ,1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
fclose(fileID);
sound = [dataArray{1:end-1}];
[X,Y] = meshgrid(-10:5:25, -15:5:100);
surf(X,Y,sound)

サインインしてコメントする。


Star Strider
Star Strider 2019 年 5 月 16 日
編集済み: Star Strider 2019 年 5 月 17 日
If you have R2019a, use the readmatrix (link) function.
You are supposed to use the contour function for the plot.
EDIT —
D = readmatrix('sound.txt');
A = D(2:end,2:end);
X = D(2:end,1);
Y = D(1,2:end);
figure
contourf(X',Y,A', 'ShowText','on')
grid on
xlabel('X')
ylabel('Y')
colorbar
how can i read data given from a txt file and plot graph - 2019 05 17.png

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by