How do I compute the same function on multiple columns within a data file, then write the column results to excel? Thanks!

조회 수: 2 (최근 30일)
I'm getting this error on the line where it says "BFrecovery(col)= (Treatment-Min)/Diff"
What I'm trying to do is 1) grab a data file from the folder 2) run the same function on columns 2:(# of columns in the data file) and 3) write the column results to excel file. However, I can't seem to achieve step 3 because of the aforementioned error. What do I need to tweak in order to write the the column results to excel file?
if true
% code
end
function [PctRecov]=BFRecov();
delimiter = ',';
startRow = 2;
fmt=[repmat('%f',1,5) '%*[^\n]'];
pn = uigetdir(pwd,'BFRecovery');
d=dir(fullfile(pn, '*.csv'));
% if no match, abort, tell user
if isempty(d),error(['DIR: no files matched ' fullfile(pn, '*.csv')]),end
% found at least one so can continue
L=length(d);
% and loop over the files...
for i=1:L
msg='';
[fid,msg]=fopen(fullfile(pn,d(i).name),'r');
error(msg) % should never fail here w/ above check
dataArray=cell2mat(textscan(fid, fmt, ...
'Delimiter', delimiter, ...
'headerlines', startRow, 'collectoutput',1));
fid=fclose(fid); %closing the file
% if no data, abort, tell user
if isempty(dataArray),error(['No data from ' fullfile(pn,d(i).name)]),end
[rows, columns] = size(dataArray);
BFrecovery=zeros(rows,4);
Time_Hrs=dataArray(:,1);
for col = 2 :columns;
Treatment= dataArray(:,col);
Max = max(Treatment);
Min =min(Treatment);
Diff=Max-Min;
BFrecovery(col)= (Treatment-Min)/Diff
end
end
xxlswrite(fullfile(pn,'BFrecovery.xls'),[BFrecovery]);

채택된 답변

Star Strider
Star Strider 2017년 5월 17일
Try this:
BFrecovery(:,col)= (Treatment-Min)/Diff
It is not possible to assign a vector (since ‘Treatment’ is a column vector) to a single element scalar array element. This change assigns it as a column of ‘BFrecovery’.

추가 답변 (1개)

Randy st
Randy st 2017년 5월 17일
perfect, thank you!

카테고리

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