Can someone explain to me how I can format the function to output only Max and Half_max into an excel file, for each file in the loop? Thanks!

function []= Mathworks_HalfMaxtimes()
% set fixed parameters
delimiter = ',';
startRow = 25;
fmt=[repmat('%s',1,2) '%*[^\n]'];
% scan directory requested
pn = uigetdir(pwd,'matlabbatch');
d=dir(fullfile(pn, '*.csv'))
dir(fullfile(pn, '*.csv'))
% loop over found files
for i=1:length(d)
fid=fopen(d(i).name,'r');
dataArray = textscan(fid, fmt, 'Delimiter', delimiter, ...
'headerlines', startRow, ...
'ReturnOnError', false);
fid=fclose(fid);
%%Allocate imported array to column variable names
Time_Hrs = dataArray{:, 1};
Resis_Cap = dataArray{:, 2};
Time_Hrs1=str2double(Time_Hrs);
Resistance_Ohm1=str2double(Resis_Cap);
%Plot Orginal Figure
figure( 'Name', 'Orig' );
h = plot( Time_Hrs1, Resistance_Ohm1);
legend( h, 'RorC vs Time', 'untitled fit 1', 'Location', 'NorthEast' );
% Perform the 1/2 Time Operation
MaxHalf= max(Resistance_Ohm1)/2; %value to find
Max= max(Resistance_Ohm1)
tmp = abs(Resistance_Ohm1-MaxHalf);% Taking the capicitance values and subtracting the 1/2 max from the vector
[minimum idx] = min(tmp); %index of closest value, finding the row at which the value is closest to 0
Time_Half = Time_Hrs1(idx) %closest time;
% Label axes with half max
xlabel('Time(hrs)')
ylabel('ResistanceorCapacitance')
grid on
deriv=h
hold on
plot([Time_Half,Time_Half], [0,MaxHalf]);
str= ('Half-max');
text(Time_Half,MaxHalf, str)
A=xlswrite('Half_Max.xlsx', Max);
end

 Risposta accettata

OK, after all the above discussion, here's the function I tested on a file with two CSV-separated numeric columns.
It also has at least rudimentary error checking to ensure
  1. a file is found, hence will be opened
  2. the data read succeeded to the point at least DataArray isn't empty
Hopefully that will be enough to get you started...btw, there's a third variable calculated that isn't in your return list if you care about perhaps having it available other than in the Excel file...
As noted, the only logical explanation for the previous assignment error is that the vector was empty which I had not considered; besides "just knowing" the code would work if appropriate file(s) were available, I had presumed you would have checked that the reading portion was working, at least.
function [RMax, RMax_Half]= Mathworks_HalfMaxtimesRational()
delimiter = ',';
startRow = 25;
fmt=[repmat('%f',1,2) '%*[^\n]'];
pn = uigetdir(pwd,'matlabbatch');
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);
RMax=zeros(L,1);
RMax_Half=RMax;
RTime_Half=RMax;
% 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
Time_Hrs=dataArray(:,1);
Res=dataArray(:,2);
RMax(i)= max(Res);
RMax_Half(i)=RMax(i)/2;
[~,idx] = min(abs(Res-RMax_Half(i)));
RTime_Half(i) = Time_Hrs(idx);
end
xlswrite(fullfile(pn,'Time_Half_Max.xls'), [RMax RMax_Half RTime_Half])
fprintf('Time_Half_Max.xlsx\n')
fprintf('%.2f %.2f %.2f\n', [RMax RMax_Half RTime_Half].') % what wrote
PS: Can't overemphasize how valuable the debugger is--and it's trivial to use; just click on a line in the LH margin by the line numbers and start the function from the command line. You can then explore what's happening as you step through, seeing variables as they are inside the function to find such problems far more quickly than we can do so here...

7 Commenti

First let me say thank you for all your help so far!
It's now saying that no data is in my file ,although there is data. This might be because the first two columns in my excel sheet have the heading 'text' and not 'number'( this is because of the nature of the machine I'm using to generate this file. This is why I converted to double in the first place.....?
also I don't understand why you need to specify the folder name in the code, when it prompts you to browse the file manually?
Because if you aren't IN that folder, since_d(i).name_ returns the file name, NOT the fully-qualified name and fopen won't find it.
Compare value of pn and what pwd returns; if they're not the same, then either you'll open the wrong file of the same name if a file of that name happens to exist in working directory or the fopen will fail if it doesn't. Either is bad; first case you're not working on the file you think you are; second you haven't even opened a file.
The latter of which is why one should always check return status; generally in Answers forum we tend to gloss over such as being distractions to a real problem expecting the requestor to take care of the nitty-gritty details.
"Use the debugger, Luke!"
ADDENDUM
help uigetdir
...
uigetdir Standard open directory dialog box
DIRECTORYNAME = uigetdir(STARTPATH, TITLE)
displays a dialog box for the user to browse [...]
and returns the directory name as a string....
NB: It doesn't say anything about actually changing working directory or anything like that...it only provides the selected directory name string; up to the application to make use of that however it needs/wants...
As for the file, way, way, way back I wondered that there may be an "off by one" error in the 'headerlines' count just reading that initial auto-generated code.
I was sure I had mentioned that, but looking back, I can't seem to find where so mayhaps I didn't but just thought had.
Anyway, if you look at the file, the column header is in line 26; the data start in row 27; but are using 'headerlines',25 so it's trying to read that header as a number and fails. In the original auto-generated code, it was read as string so didn't matter but made the conversion later in str2double. Why is anybody's guess altho if do read as the cellstr array if the column header is significant can use it to create a table or to label output...
Anyways, again, the key is to use error-checking and the debugger to find why things aren't working as expected when results aren't those expected...
Fix it to match the actual file format.
It works! Thanks a lot! I'm currently taking a MATLAB basics course and I hope to be a bit more competent at the end:)

Accedi per commentare.

Più risposte (1)

As I suggested earlier, you need to add some array handling/outputs to your function...
MaxHalf=zeros(length(d),1); % preallocate result array
Max=MaxHalf; % ditto...
for i=1:length(d)
...
% Perform the 1/2 Time Operation
MaxHalf(i)=max(Resistance_Ohm1)/2;
Max(i)= max(Resistance_Ohm1)
...
end
xlswrite('HalfMax_Max.xlsx', [Max MaxHalf])
ADDENDUM
Of course, having written one, the other is pretty easy to derive! :) (Of course, need to know which it is if only one in the file; pretty easy to tell with both! :) )
xlswrite is quite slow so save up all the values and write in "one swell foop" at the end instead of calling every time. Plus, if did the other way, would have to keep an R,C location to prevent overwriting the same data every time.

19 Commenti

Ok so I tried to incorporate what you said into what I was trying to do. However, it didn't work for me at all.
Also, note that I am new to both matlab and programming.
I only want to save Max and Time_Half to excel
function [Max, Time_Half]= Mathworks_HalfMaxtimes()
% set fixed parameters
delimiter = ',';
startRow = 25;
fmt=[repmat('%s',1,2) '%*[^\n]'];
% scan directory requested
pn = uigetdir(pwd,'matlabbatch');
d=dir(fullfile(pn, '*.csv'))
dir(fullfile(pn, '*.csv'))
% loop over found files
for i=1:length(d)
fid=fopen(d(i).name,'r');
dataArray = textscan(fid, fmt, 'Delimiter', delimiter, ...
'headerlines', startRow, ...
'ReturnOnError', false);
fid=fclose(fid);
%%Allocate imported array to column variable names
Time_Hrs = dataArray{:, 1};
Resis_Cap = dataArray{:, 2};
Time_Hrs1=str2double(Time_Hrs);
Resistance_Ohm1=str2double(Resis_Cap);
%Plot Orginal Figure
figure( 'Name', 'Orig' );
h = plot( Time_Hrs1, Resistance_Ohm1);
legend( h, 'RorC vs Time', 'untitled fit 1', 'Location', 'NorthEast' );
Max=zeros(length(d),1);% preallocate result arrays for Max and Time_Half
Time_Half=zeros(length(d),1);
% ditto...
for i=1:length(d)
% Perform the 1/2 Time Operation
MaxHalf =max(Resistance_Ohm1)/2;
Max(i)= max(Resistance_Ohm1);
tmp= abs(Resistance_Ohm1-MaxHalf(i));% Taking the capicitance values and subtracting the 1/2 max from the vector
[minimum idx] = min(tmp(i)); %index of closest value, finding the row at which the value is closest to 0
Time_Half(i) = Time_Hrs1(idx);
end
xlswrite('Time_Half_Max.xlsx', [Max(i) Time_Half(i)])
% MaxHalf= max(Resistance_Ohm1)/2; %value to find
% Max= max(Resistance_Ohm1);
%tmp = abs(Resistance_Ohm1-MaxHalf);% Taking the capicitance values and subtracting the 1/2 max from the vector
% [minimum idx] = min(tmp); %index of closest value, finding the row at which the value is closest to 0
%Time_Half = Time_Hrs1(idx); %closest time;
% Label axes with half max
% xlabel('Time(hrs)')
% ylabel('ResistanceorCapacitance')
% grid on
%deriv=h
%hold on
%plot([Time_Half,Time_Half], [0,MaxHalf]);
% str= ('Half-max');
%text(Time_Half,MaxHalf, str)
end
I made a few mistakes on the Time_Half(i) variable, but I think you can see what I was trying to do. Besides the Max(i) doesn't write to Excel either. It basically copies the same value over and over.
Yeah, you've still got the xlswrite inside the loop instead as I showed, after the loop to write the whole array in "one swell foop".
So, you've ovewritten the same location (A1) every pass.
function [Max, Time_Half]= Mathworks_HalfMaxtimes()
delimiter = ',';
startRow = 25;
fmt=[repmat('%f',1,2) '%*[^\n]'];
pn = uigetdir(pwd,'matlabbatch');
d=dir(fullfile(pn, '*.csv'));
L=length(d);
for i=1:L
fid=fopen(d(i).name,'r');
dataArray=cell2mat(textscan(fid, fmt, 'Delimiter', delimiter, ...
'headerlines', startRow ...
'collectoutput',1));
fid=fclose(fid);
Time_Hrs = dataArray(:,1);
Resis_Cap = dataArray(:,2);
Max=zeros(L,1);
MaxHalf=Max;
Time_Half=Max;
for i=1:length(d)
MaxHalf(i)=max(Res_Ohm1)/2;
Max(i)= max(Res_Ohm1);
tmp=abs(Res_Ohm1-MaxHalf(i));
[~,idx] = min(tmp);
Time_Half(i) = Time_Hrs1(idx);
end
xlswrite('Time_Half_Max.xlsx', [Max Time_Half])
end
Let's "simplify, simplify"...cut it down to the bare basics first and get that working; THEN you can do other things without all the obfuscation hindering seeing the trees for the forest.
First, since you don't do anything but str2double on the values returned, there's no need to have read them as strings to begin with...change the format string to '%f' instead and do the conversion when reading the file.
Second, you haven't defined ResistanceOhm1--what is it and where does it come from? Need that before anything will work. Note that I shortened the name significantly; excessively-long variable names just clutter up the code by adding more noise than they clarify anything. Keep meaningful names, 'a' isn't meaningful, but something 20 characters long is just too hard to keep straight.
What the rest is supposed to be and what it needs to be to accomplish that depends entirely upon what that variable is--is it fixed over all files, is it a constant that changes for each file, is it a vector of values for each file, ...???
Without that, there's nowhere else to go.
Once one gets that done, then you can go back and and in some of the plotting and other niceties, but got to solve first things first.
Hey I'm getting this error message: "In an assignment A(:) = B, the number of elements in A and B must be the same."
I'm thinking it might have something to do with the preallocation RMax...?
function [RMax, RMaxHalf]= Mathworks_HalfMaxtimes2()
%Grabbing your folder,grabbing files of interest, assigning length of folder, formatting documents,
delimiter = ','; %The comma is used to separate matrix subscripts and arguments to functions.
startRow = 25; %specifying the first row you want data from
fmt=[repmat('%f',1,2) '%*[^\n]']; %The fmt variable is the format string that maps the content of the file to the data variables you want returned--above it is two string variables and then an idiom to skip the rest of each record (line).
pn = uigetdir(pwd,'matlabbatch'); % Getting directory/folder from memory
d=dir(fullfile(pn, '*.csv')); %getting specific files from directory/folder
L=length(d); %assigning length of directory/folder
for i=1:L
fid=fopen(d(i).name,'r'); %opening file # i
dataArray=cell2mat(textscan(fid, fmt, 'Delimiter', delimiter, 'headerlines', startRow, 'collectoutput',1));
fid=fclose(fid); %closing the file
%Assigning parts of array to designated variables, must set up excel file accordingly
Time_Hrs = dataArray(:,1);
Res = dataArray(:,2);
%Preallocate memory for each variable of interest, with dimension of file directory L ,to enhance matlab efficiency to avoid reopening file each iteration
RMax=zeros(L,1);
RMaxHalf=RMax;
RTime_Half=RMax;
% Loop for functions that store your variables you want saved in excel
for i=1:length(d)
RMax(i)= max(Res);
RMaxHalf(i)=Res/2;
tmp=abs(Res-RMaxHalf(i));
[~,idx] = min(tmp);
RTime_Half(i) = Time_Hrs(idx);
end
xlswrite('Time_Half_Max.xlsx', [RMax RMaxHalf])
end
Well, you might tell us where, at least. Post the error in context...
Ah sorry
if true
% code
end
function [RMax, RMaxHalf]= Mathworks_HalfMaxtimes2()
%Grabbing your folder,grabbing files of interest, assigning length of folder, formatting documents,
delimiter = ','; %The comma is used to separate matrix subscripts and arguments to functions.
startRow = 25; %specifying the first row you want data from
fmt=[repmat('%f',1,2) '%*[^\n]']; %The fmt variable is the format string that maps the content of the file to the data variables you want returned--above it is two string variables and then an idiom to skip the rest of each record (line).
pn = uigetdir(pwd,'matlabbatch'); % Getting directory/folder from memory
d=dir(fullfile(pn, '*.csv')); %getting specific files from directory/folder
L=length(d); %assigning length of directory/folder
for i=1:L
fid=fopen(d(i).name,'r'); %opening file # i
dataArray=cell2mat(textscan(fid, fmt, 'Delimiter', delimiter, 'headerlines', startRow, 'collectoutput',1));
fid=fclose(fid); %closing the file
%Assigning parts of array to designated variables, must set up excel file accordingly
Time_Hrs = dataArray(:,1);
Res = dataArray(:,2);
%Preallocate memory for each variable of interest, with dimension of file directory L ,to enhance matlab efficiency to avoid reopening file each iteration
RMax=zeros(L,1); <<<ERROR
RMaxHalf=RMax;
RTime_Half=RMax;
% Loop for functions that store your variables you want saved in excel
for i=1:length(d)
RMax(i)= max(Res);
RMaxHalf(i)=Res/2;
tmp=abs(Res-RMaxHalf(i));
[~,idx] = min(tmp);
RTime_Half(i) = Time_Hrs(idx);
end
xlswrite('Time_Half_Max.xlsx', [RMax RMaxHalf])
end
MATLAB Says the following at bottom of command window: In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in Mathworks_HalfMaxtimes2 (line 25) RMax(i)= max(Res);
That doesn't seem feasible from the code posted... max() only returns a single value from a vector.
BUT, you've got much bigger issues than this, you've duplicated the code loops twice??? Why didn't you use the code I gave before? I defined L as length(d) simply to keep from calling the same function on the same array multiple times; now you've moved the preallocation code inside the loop over the files where you'll wipe out what you did for the preceding file every time and then for some unknown reason started yet another loop inside that...
Go back to the earlier version...what do you think is wrong with what I wrote as is?
That would be this specific comment <comment 451896> to be precise.
Unless you're doing something different than what you described, that should work as it stands, barring typos and any aha! moments which are always possible working from afar, of course.
Anyway, let's start with that and get it to work instead of going back to square one all the time.
Also, please learn to use the code formatting; I know it's a kluncky interface, I've bi...er, complained at length about it to TMW but it is what they've provided. (:
Either
  1. highlight the code section and then use the {}Code button, or
  2. Using two blank spaces at beginning of code line will switch out of word wrap mode so you set cursor at that point and paste.
OK, sorry I let self get upset...it's not quite as bad as first thought...most of what I had is there, you just duplicated some stuff I don't understand why...
Up to and including looks ok.
...
L=length(d); % so now preallocate knowing length...
RMax=zeros(L,1); <<<ERROR
RMaxHalf=RMax;
RTime_Half=RMax;
% and loop over the files...
for i=1:L
fid=fopen(d(i).name,'r'); %opening file # i
dataArray=cell2mat(textscan(fid, fmt, 'Delimiter', delimiter, 'headerlines', startRow, 'collectoutput',1));
fid=fclose(fid); %closing the file
Time_Hrs = dataArray(:,1);
Res = dataArray(:,2);
RMax(i)= max(Res);
RMaxHalf(i)=Res/2;
tmp=abs(Res-RMaxHalf(i));
[~,idx] = min(tmp);
RTime_Half(i) = Time_Hrs(idx);
end
xlswrite('Time_Half_Max.xlsx', [RMax RMaxHalf])
Again, the loop is over the files and you're saving one value per each file for each variable which is a vector. Matlab works on vectors, you don't need another steenkin' loop! inside the one.
Yes, sorry about that loop doubling job.
The same error is being returned as I mentioned earlier. The reason I would guess is because of the pre-allocation code you added on, which sets the dimensions of Rmax to LX1 vector, but Rmax(i) below is not a single element vector. This is probably why it worked earlier when the pre-allocation bit wasn't included....Still, is there a way to keep the pre-allocation bit since as you've said, MATLAB works better with it?Or should I just go back to how it was before the preallocation bit..?
function [RMax, RMaxHalf]= Mathworks_HalfMaxtimesRational()
%Grabbing your folder,grabbing files of interest, assigning length of folder, formatting documents,
delimiter = ','; %The comma is used to separate matrix subscripts and arguments to functions.
startRow = 25; %specifying the first row you want data from
fmt=[repmat('%f',1,2) '%*[^\n]']; %The fmt variable is the format string that maps the content of the file to the data variables you want returned--above it is two string variables and then an idiom to skip the rest of each record (line).
pn = uigetdir(pwd,'matlabbatch'); % Getting directory/folder from memory
d=dir(fullfile(pn, '*.csv')); %getting specific files from directory/folder
L=length(d); % so now preallocate knowing length...
RMax=zeros(L,1);
RTime_Half=RMax;
% and loop over the files...
for i=1:L
fid=fopen(d(i).name,'r'); %opening file # i
dataArray=cell2mat(textscan(fid, fmt, 'Delimiter', delimiter, 'headerlines', startRow, 'collectoutput',1));
fid=fclose(fid); %closing the file
Time_Hrs = dataArray(:,1);
Res = dataArray(:,2);
RMax(i)= max(Res);
RMaxHalf(i)=Res/2;
tmp=abs(Res-RMaxHalf(i));
[~,idx] = min(tmp);
RTime_Half(i) = Time_Hrs(idx);
end
xlswrite('Time_Half_Max.xlsx', [RMax RTime_Half])
To clarify, the error
"In an assignment A(:) = B, the number of elements in A and B must be the same.", is at RMax(i)=max(Res) line.
"...but Rmax(i) below is not a single element vector"
This makes no sense at all.
Res = dataArray(:,2);
Res will be a column vector by the above; selects all rows of the second column.
Ergo, max(Res) will be a single value, the maximum of that vector unless you've somewhere aliased max the builtin function.
Thus, there can be no more than one element to be assigned to RMax(i)
Whatever it is, it isn't that nor the preallocation; it's somewhere you're not showing in enough context to be able to see.
Demo:
>> Res=randn(30,1); % simulate your vector
>> max(Res) % max() is just one value
ans =
3.5784
>> Res([3 5 23])=Res(1); % make some duplicates
>> max(Res) % and still only the one returned
ans =
3.5784
>>
So, as said, it isn't what you think; set a breakpoint before that line and look at sizes of all variables up to that point and see where something isn't what think it is.
Of course,
which max
at the command line wouldn't hurt to see what you're actually executing when max is called. If it isn't the builtin, "Houston, we have a problem!"
clear max
will take care of it if you have somehow managed to do that...
OK, I took your code and pasted it into the editor so could format it legibly...and then can see what overlooked before...
The problem is related to what I asked before about what you intended that never resolved...
...
RMaxHalf(i)=Res/2;
There's the problem; Res is still the vector; what is RMaxHalf supposed to be...is it just
RMaxHalf(i)=Rmax(i)*/2;
??? Why did you leave the whole vector there I asked before quite some time back...
Then, what's the intent of the next stuff with
tmp=abs(Res-RMaxHalf(i));
[~,idx] = min(tmp);
??? I don't know what you're trying to compute here, but you've got similar problem in mixing up what are single values with vectors.
Need the definition of what the results are supposed to be first instead of just trying to make something run.
even if I change that part of the script, it gives the same error. Are you sure that by preallocating RMax as a column vector ( L, 1), that this isn't causing problems trying to assign a single element max(Res) into RMax(i)
if true
% code
end
RMaxHalf(i)=max(Res)/2;
tmp=abs(Res-RMaxHalf(i));
[~,idx] = min(tmp);
RTime_Half(i) = Time_Hrs(idx);
end
xlswrite('Time_Half_Max.xlsx', [RMax RTime_Half])
what i'm doing is taking the column vector Res and subtracting a single value from all elements of the vector. this single value is RMaxHalf(i).
On the first, yes, I'm sure, I just demonstrated to you it isn't the issue. The problem is the next line where you try to assign
RMaxHalf(i)=Res/2;
Why would you take max(Res) twice in succession in two lines of code???? You had just found the max value and saved it; why make the computer do all the work to find the same number twice???
RMax(i)= max(Res);
RMaxHalf(i)=RMax(i)/2;
is identically the same value at zero additional cost.
The subsequent lines ending may be ok; it will execute but not sure what the min() of that difference means, but it's certainly legal to do what you've done albeit "the Matlab way" would do without the temporary.
[~,idx] = min(abs(Res-RMaxHalf(i)));
RTime_Half(i) = Time_Hrs(idx);
Even if I change that, it doesn't matter. The error occurs before that.
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in Mathworks_HalfMaxtimesRationalathome (line 18) RMax(i)= max(Res);
Put lines
size(Res)
size(max(Res))
whos Rmax
just before that line then and show the full context of the error including what those lines show and the code EXACTLY as you ran it...it's gotta' be something not being shown 'cuz that can't error that way as I showed in previous demo--if Res is a vector, then max(Res) must be a single value.
ADDENDUM
OK, I ran the copy of the code I created here...there's one way to get the mismatch in sizes I hadn't thought of--that's if Res is empty; iow, you didn't successfully read any data...I never use the directory selection as you did (not that that's necessarily bad, it's just not the way I code) so if you're not in the directory choose, then the fopen may fail or the .csv files aren't necessarily the ones wanted and I presumed from the dir it'd be guaranteed.
What need is where did
d=dir(fullfile(pn, '*.csv'));
and used specific path,
fid=fopen((fullfile(pn,d(i).name,'r'));
do same thing in the fopen call as well to be same files. You also need to do same on the filename in the xlswrite call, too, if that's where you want that file, otherwise it'll end up in the current working directory instead.
That SHOULD fix your problem; the above diagnostics will point to the issue likely; the other possibility is the textscan call didn't succeed if the data aren't matching up to the format string.
A test on the size(dataArray) would let you check on that, but just use debugger after the above fixup and ensure you're reading the files you think you are and the data are actually being read in.
I just never thunk of it being empty...empty is certainly also not the same size as one. :)
But, lesson is, USE THE DEBUGGER!! when things aren't making sense.
PS: If you want this RMaxHalf() array, too, then you also need to preallocate it.
Here is the code I currently have
if true
% code
end
%Grabbing your folder,grabbing files of interest, assigning length of folder, formatting documents,
delimiter = ','; %The comma is used to separate matrix subscripts and arguments to functions.
startRow = 25; %specifying the first row you want data from
fmt=[repmat('%f',1,2) '%*[^\n]']; %The fmt variable is the format string that maps the content of the file to the data variables you want returned--above it is two string variables and then an idiom to skip the rest of each record (line).
pn = uigetdir(pwd,'matlabbatch'); % Getting directory/folder from memory
d=dir(fullfile(pn)); %getting specific files from directory/folder
L=length(d); % so now preallocate knowing length...
RMax=zeros(L,1);
RTime_Half=RMax;
% and loop over the files...
for i=1:L
fid=fopen(d(i).name,'r'); %openingdbs file # i
dataArray=cell2mat(textscan(fid, fmt, 'Delimiter', delimiter, 'headerlines', startRow, 'collectoutput',1));
fid=fclose(fid); %closing the file
Time_Hrs = dataArray(:,1);
What changes do I need to make? I'm not sure how dir and fopen work , that part of the code was given to me...
"I'm not sure how dir and fopen work , that part of the code was given to me..."
Read the doc to learn how they work; don't rely on somebody else entirely.

Accedi per commentare.

Richiesto:

il 5 Mag 2017

Modificato:

dpb
il 11 Mag 2017

Community Treasure Hunt

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

Start Hunting!

Translated by