Clear Filters
Clear Filters

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

2 views (last 30 days)
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]);

Accepted Answer

Star Strider
Star Strider on 17 May 2017
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’.

More Answers (1)

Randy st
Randy st on 17 May 2017
perfect, thank you!

Community Treasure Hunt

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

Start Hunting!