Read excel data but keep formatting? Is this possible?
Afficher commentaires plus anciens
I have a huge excel data file of data collected every second and the time column is in the format 12:12:56, But when i do an xlsread I do not get the same string. Is there a way to import this properly?
Réponses (2)
Sara
le 6 Mai 2014
Use this form of xlsread, you'll find your variable into txt or raw:
[num,txt,raw] = xlsread(___)
From the help: [num,txt,raw] = xlsread(_) additionally returns the text fields in cell array txt, and the unprocessed data (numbers and text) in cell array raw using any of the input arguments in the previous syntaxes. If xlRange is specified, leading blank rows and columns in the worksheet that precede rows and columns with data are returned in raw.
25 commentaires
matlabuser12
le 6 Mai 2014
Roberto
le 6 Mai 2014
I can't see your attached file! please try uploading it again...
matlabuser12
le 6 Mai 2014
Sara
le 6 Mai 2014
You're right, since it's a custom format it does not read it as it shows it. Use:
datestr(num,'HH:MM:SS PM')
where num is just the first column in the excel file.
matlabuser12
le 6 Mai 2014
Sara
le 6 Mai 2014
Let's see if I have understood. You read the data in, then do you want the user to select only specific times or a range?
matlabuser12
le 6 Mai 2014
Sara
le 6 Mai 2014
Save your file as csv and try the code below
% User input
str = 'Rate';
init_time = '12:45:41 ';
end_time = '12:47:32';
[time,storage] = findmyentries(str,init_time,end_time);
figure
plot(datenum(time),storage)
y = get(gca,'xtick');y = y(1:2:end);
set(gca,'xtick',y,'xticklabel',datestr(y,'HH:MM:SS'))
function [time,storage] = findmyentries(str,init_time,end_time)
init_time = datenum(init_time);
end_time = datenum(end_time);
filename = 'ddd.csv'; %%REPLACE
[fid,msg] = fopen(filename,'r');
if(fid==-1),error(msg),end
header = DivideFields(fid);
n = find(~cellfun(@isempty,strfind(header,str))==1,1);
if(isempty(n)),error('field not found');end
fgetl(fid);
max_el = 100;
time = cell(max_el,1);
storage = zeros(max_el,1);
k = 0;
while 1
var = DivideFields(fid);
if(isempty(var{1})),break,end
isbetween = CompareTime(var{1},init_time,end_time);
if(isbetween == 1)
k = k + 1;
if(k > max_el)
max_el = max_el + 100;
t = time;time = cell(max_el,1);time(1:k-1) = t;
t = storage;storage = zeros(max_el,1);storage(1:k-1) = t;
end
time{k} = var{1};
storage(k) = str2num(var{n});
elseif(isbetween == 2)
break
end
end
time = time(1:k);
storage = storage(1:k);
fclose(fid);
function out = DivideFields(fid)
out = fgetl(fid);
if(~isempty(out))
out = textscan(out,'%s','Delimiter',',');
out = out{1};
end
function isbetween = CompareTime(var,init_time,end_time)
current_time = datenum(var);
if(current_time >= init_time && current_time <= end_time)
isbetween = 1;
elseif(current_time > end_time)
isbetween = 2;
else
isbetween = 0;
end
matlabuser12
le 7 Mai 2014
Modifié(e) : matlabuser12
le 7 Mai 2014
Sara
le 7 Mai 2014
Did you save your excel as csv before running the code? It won't work otherwise.
matlabuser12
le 7 Mai 2014
Sara
le 7 Mai 2014
I get the answer I think you wanted...can you attach your csv so I can check what's different?
matlabuser12
le 7 Mai 2014
Sara
le 7 Mai 2014
My bad. Replace:
[header,~] = DivideFields(fid);
[var,eof] = DivideFields(fid);
and
function [out,eof] = DivideFields(fid)
out = fgetl(fid);
eof = 0;
if(out == -1)
out = [];
eof = 1;
elseif(~isempty(out))
out = textscan(out,'%s','Delimiter',',');
out = out{1};
end
Now it should work.
matlabuser12
le 8 Mai 2014
Sara
le 8 Mai 2014
I'm attaching the file so that I don't forget anything this time...modify as it pleases you.
matlabuser12
le 8 Mai 2014
Modifié(e) : matlabuser12
le 8 Mai 2014
Sara
le 8 Mai 2014
If you know how many lines of headers you have, you can just skip them:
for i = 1:25
fgetl(fid);
end
So, do you know that it will always be 25?
For the second point, do you mean that in between the data you could have "Time Rate Pressure" repeated if you stop and restart the data acquisition or also the 25 extra lines?
matlabuser12
le 8 Mai 2014
Sara
le 8 Mai 2014
That's what I would do:
- Search for the string time (to know when start reading data)
- While reading, check that suddenly there is no empty cell or char
- do not stop (as the code does now) but rather keep going until you find "time" again
- repeat
I would use a while loop and read a max of 500 lines with no numeric data, so that you have a way to stop the code when you actually reach the end of file. I suppose none would use a header of 500 lines! Take a shot at it, in case you can post again if you go nowhere :) As for the code, it's for you to modify as you please.
matlabuser12
le 8 Mai 2014
matlabuser12
le 8 Mai 2014
Sara
le 8 Mai 2014
Jose meant that you can use activex in matlab. that's a way to do it too, but I have never used it to read from excel, only to write. you may want to look into it anyway.
matlabuser12
le 20 Mai 2014
matlabuser12
le 20 Mai 2014
2 commentaires
Sara
le 20 Mai 2014
Can you attach a draft of the code you are using? I suppose you started modifying things.
matlabuser12
le 20 Mai 2014
Catégories
En savoir plus sur Data Import from MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!