How to plot graph using data from a text file?

Attached is the sound.txt file and questions.
How do we plot a graph with said x and y values? i have tried this but to no success.
filename='sound.txt';
fid= fopen(filename,'r');
header = fscanf(fid,'%s',[1 1]);
headerx= fscanf(fid,'%f',[1 8]);
headery=[];
data=[];
for k= 1:24
headery= [headery; fscanf(fid,'%f',[1 1])];
data = [data; fscanf(fid,'%f',[1 8])];
end
mesh(headerx,headery,data)
Help please :)

8 件のコメント

Geoff Hayes
Geoff Hayes 2019 年 5 月 15 日
bala - what is wrong with the plot from mesh? Is that not what you are expecting?
Star Strider
Star Strider 2019 年 5 月 15 日
Use readmatrix to import the file. (If you do not have readmatrix, then textscan would probably be your next most appropriate alternative.) You then have to remove the top row and the first column and save them separately (those will be your x and y coordinates respectively), and plot the rest of the matrix.
This is a MATLAB programming assignment, so it would not be appropriate to give specific help on the rest of it.
bala balu
bala balu 2019 年 5 月 16 日
I was looking to plot a graph with the said x and y coordinates as the scale. I mistakenly did a mesh graph. It’s supposed to be a normal 2D graph with the said x and y coordinates. I’m very lost on how to plot out a graph with the said x and y scales. I did the textscan but how do you use the plot function with those x and y coordinates. I’m very new to Matlab :D
bala balu
bala balu 2019 年 5 月 16 日
I don’t understand what you mean by saving the x and y coordinates separately, do you mean save it to another text file? How do I use the plot function based on your suggestion?
Star Strider
Star Strider 2019 年 5 月 16 日
I believe the assignment wants you to do a contour plot. The coding for that is similar to a mesh plot.
Save the x and y values as vectors in your workspace. Save the rest of the data as a matrix.
bala balu
bala balu 2019 年 5 月 16 日

Do you means using like X=[-10:5:25] when you meant saving it as a vector? And can you please clarify what you meant by saving the rest as a matrics? How do you accomplish that?

Star Strider
Star Strider 2019 年 5 月 16 日
Do you means using like X=[-10:5:25] when you meant saving it as a vector?
Yes. Although you need to read it (and the y-vector) from the file.
And can you please clarify what you meant by saving the rest as a matrics? How do you accomplish that?
The entire array will be imported as a matrix. For the rest, see the documentation section on Matrix Indexing (link).
bala balu
bala balu 2019 年 5 月 16 日
I have successfully completed my assignment. Thanks for all the help!

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

 採用された回答

KSSV
KSSV 2019 年 5 月 16 日

2 投票

Replace y/x in the text file with NaN.
A = load('sound.txt') ;
x = A(1,2:end) ;
y = A(2:end,1) ;
Z = A(2:end,2:end) ;
figure
surf(x,y,Z)
figure
pcolor(x,y,Z)
figure
contour(x,y,Z)

3 件のコメント

bala balu
bala balu 2019 年 5 月 16 日
Okay I shall try this and see if I have any luck, will update here ASAP.
bala balu
bala balu 2019 年 5 月 16 日
Thank you, i managed to produce a graph with this.
Peter Arandorenko
Peter Arandorenko 2020 年 7 月 16 日
Can you show us how it looks like?

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGraphics Object Properties についてさらに検索

質問済み:

2019 年 5 月 15 日

コメント済み:

2020 年 7 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by