필터 지우기
필터 지우기

how to import data from csv and plot?

조회 수: 144 (최근 30일)
Ani Asoyan
Ani Asoyan 2022년 11월 9일
댓글: Ani Asoyan 2022년 11월 9일
Hi everyone!
I want to import excel dataset (csv file) to matlab and plot.
The first column includes string variables like "2022Q1" , "2022Q2", etc.
The second column is regular data. How can I import that csv and plot 2nd column with respect to 1st column (so the strings be the horizontal axis).
I've tried something like this but doesn't work.
[Array, str, raw]=xlsread('baseline.csv');
Error using xlsread
Unable to open file 'baseline.csv'.
File '/users/mss.system.fjCCc6/baseline.csv' not found.
y= Array(:, 2);
x= Array (:, 1);
plot(x,y)
I'm attaching excel file also.
Thank you

채택된 답변

Arif Hoq
Arif Hoq 2022년 11월 9일
A=readtable('baseline.csv','VariableNamingRule','preserve');
xaxis=table2array(A(:,1));
datavalue=table2array(A(:,2));
C=categorical(xaxis);
plot(C,datavalue)
xtickangle(90)
  댓글 수: 3
Arif Hoq
Arif Hoq 2022년 11월 9일
just use this syntax
datavalue=A.d_qoil_t;
Ani Asoyan
Ani Asoyan 2022년 11월 9일
thank you so much !

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

추가 답변 (2개)

KSSV
KSSV 2022년 11월 9일
T=readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1186578/baseline.csv');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
X=T.(1);
Y=T.(2);
plot(Y)
  댓글 수: 1
Ani Asoyan
Ani Asoyan 2022년 11월 9일
Thank you for the answer. However I would like to put the string data in the horizontal axis, so it appears 2022Q1, 2022Q2 instead of numbers. Also, did you change the column headers in the excel file?

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


Kalpesh Bagmar
Kalpesh Bagmar 2022년 11월 9일
Hello Ani,
May be you can try following code snippet
[a]=readcell('baseline.csv')
a=a(4:end,:) % removing unwanted headings from data
b = cell2mat(a(:,2))
c = a(:,1)
bar(b)
set(gca,'xticklabel',c)
  댓글 수: 1
Ani Asoyan
Ani Asoyan 2022년 11월 9일
Thank you for your answer! I already accepted the prevous answer, but will also try your method. Thanks

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

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by