Store .csv data and calculate average value
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Goodmorning to everybody.
Can someone help me to understand how I can save in matlab 10 .csv files, select only the columns in which I am interested and get as output a final plot in which I have the average value of the y coloumns and standard deviation of y axes? I am not so good in matlab and so I kindly ask if someone to help me to solve this question.
Thanks in advance.
채택된 답변
Walter Roberson
2015년 9월 12일
Roughly,
which_column = 7; %for example
for K = 1 : 10
filedata = csvread(FileNames{K})
columndata(:,K) = filedata(:,which_column);
end
columnavg = mean(columndata,2);
columnstd = std(columndata,2);
xvals = 1 : size(columnavg,1);
plot(xvals, columnavg, 'b-', xvals, columnavg-columnstd, 'r--', xvals, columnavg+columstd, 'r--');
This would plot the average and would also plot the positions that are 1 standard deviation above and below the average.
댓글 수: 11
Snapshot83
2015년 9월 12일
편집: Snapshot83
2015년 9월 13일
I get this error:
>> Untitled
Undefined variable "FileNames" or class "FileNames".
Error in Untitled (line 3)
filedata = csvread(FileNames{K})
Another question is: In "which_column" I need to add my y axe? The coloumns I am interested are the 4th (x axe) and the 5th (Y axe)
You would store the names of the csv files as a cell array of strings in FileNames . Those names might possibly be found by using dir() like is shown in the link. For example,
FileNames = {'first.csv', 'File02.csv', 'data3good.csv', '004-very-important.txt'};
or
dirstats = dir('*.csv');
FileNames = {dirstats.Name};
Are you x axis values (column 4) the same for all of the files? If not then there is more work to do in order to form the average. In the code above you would assign which_column=5 to average the Y values.
Snapshot83
2015년 9월 13일
편집: Snapshot83
2015년 9월 13일
X axis values are the same for all the files. Can kindly tell me where I need to put this two lines? Just after the declaration of k? (after K = 1 : 10 ?)
If I write the code on this way
which_column = 5;
for K = 308:317
dirstats = dir('*.csv');
FileNames = {dirstats.Name};
filedata = csvread(FileNames(K))
columndata(:,K) = filedata(:,which_column);
end
columnavg = mean(columndata,2);
columnstd = std(columndata,2);
xvals = 1 : size(columnavg,1);
plot(xvals, columnavg, 'b-', xvals, columnavg-columnstd, 'r--', xvals, columnavg+columstd, 'r--');
I obtain this error:
>> Untitled
Reference to non-existent field 'Name'.
Error in Untitled (line 4)
FileNames = {dirstats.Name};
Assuming that you are implying that the names are 308.csv 309.csv up to 317.csv
x_column = 4;
y_column = 5;
file_range = 308:317;
for K = 1 : length(file_range)
filename = sprintf('%d.csv', file_range(K));
filedata = csvread(filename)
columndata(:,K) = filedata(:,y_column);
end
columnavg = mean(columndata,2);
columnstd = std(columndata,2);
%since the x are all the same we can use the data from the last file we read
xvals = filedata(:,x_column);
plot(xvals, columnavg, 'b-', xvals, columnavg-columnstd, 'r--', xvals, columnavg+columstd, 'r--');
Snapshot83
2015년 9월 14일
편집: Snapshot83
2015년 9월 14일
Hi. Yesterday was able to running the following code but today I have this error and I don0t know how I can solve: The error is:
Attempted to access num(:,3); index out of bounds because size(num)=[0,0].
Error in csvBatchRead (line 10)
col3=num(:,3);
Instead, the code I used is:
clear all;
clc;
which_column = 5;
dirstats = dir('*.csv');
col3Complete=0;
col4Complete=0;
for K = 1:length(dirstats)
[num,txt,raw] = xlsread(dirstats(K).name);
col3=num(:,3);
col4=num(:,4);
col3Complete=[col3Complete;col3];
col4Complete=[col4Complete;col4];
avgVal(K)=mean(col4(:));
end
col3Complete(1)=[];
col4Complete(1)=[];
%columnavg = mean(col4Complete);
%columnstd = std(col4Complete);
% xvals = 1 : size(columnavg,1);
% plot(xvals, columnavg, 'b-', xvals, columnavg-columnstd, 'r--', xvals, columnavg+columstd, 'r--');
B = reshape(col4Complete,[5000,K]);
m=mean(B,2);
C = reshape (col4Complete,[5000,K]);
S=std(C,0,2);
One of your csv files has no numeric values.
I Checked, all the files have numeric values
At the MATLAB command line, give the command
dbstop if error
then run the program and wait for it to fail. When it fails, look at dirstats(K).name which will be the name of the current file. You can look at size(num), size(txt), size(raw)
I speculate that one of your .csv files is empty.
Actually If I open the matrix K, it has only one value (1).
K is not intended to be a matrix. K is a loop counter. You are having problems on the first file. Show us
dirstats(K).name
dirstats(K).bytes
size(num)
size(txt)
size(raw)
Snapshot83
2015년 9월 14일
편집: Snapshot83
2015년 9월 14일
Now it works, thanks. My basic error was that I didn't delete an old file inside the folder so I didn't process properly my data.
I have a second problem. How I can copy this data in an excel file properly? I used the function xlswrite but maybe something is wrong. If I write
xlswrite('file1.xlsx', m),
for exemple, I am not able to see the file created inside the folder.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
