How to save data in multiple sheets with xlswrite?
2 次查看(过去 30 天)
显示 更早的评论
Hi, I would like to save data from a cell with xlswrite into multiple sheets of the workbook.
In sheet 1 I would like to save the data from year one, in sheet 2 data from year two, and so on.
My code looks like this:
for f=1:length(duniqyear)
empty=zeros(1,length(A));
j=0;
X=regexprep(Y(f,:),Y(f,:),sprintf('%s$0','Z'))
Z=strfind(A,X);
for k=1:length(A)
indx=find(Z{k});
if indx==1;
empty(k)=j+1;
end
end
C=find(empty);
B=A(C);
xlswrite(b,B,f)
end
Where
duniqyear =
2008
2009
2010
It is almost working. I need to find the data that matches the year in duniqyear. The problem is that when using
Z=strfind(A,X);
Z returns values that matches all strings containing '2008' and '2009' and so on. This is not what I want as some strings have '200808042230Z' and '02010KT' Groups. I only want to match the expression with the first Group that has information of the year of the string.
The cell A looks like this:
...
'METAR ESSB 201002282250Z AUTO 07006KT 5000NDV BR OVC001/// 01/01 Q0993 3071//73'
'METAR ESSB 201002282320Z AUTO 07008KT 2900NDV BR VV002 01/01 Q0992 3071//73'
'METAR ESSB 201002282350Z AUTO 07008KT 4200NDV BR VV002 01/01 Q0992 3071//73'
... and so on
I would like to use strfind with both '2010' and 'Z' to identify the year that the string contains. Is this possible or am I on the wrong track here?
Thank you!
2 个评论
Guillaume
2016-10-11
编辑:Guillaume
2016-10-11
It's hard to understand your code, not helped by the fact that none of the variable names have meaning, apart from the empty variable which is a name you should not use (as it's an important matlab function).
I certainly don't understand that line
X=regexprep(Y(f,:),Y(f,:),sprintf('%s$0','Z'))
which looks like a variation on the answer I gave to one of your question but simply prepends 'Z' to the strings. If that's the idea:
zstrings = regexprep( Y(f, :), '.*', 'Z$0');
%or
zstrings = cellfun(@(s) ['Z' s], Y(f, :), 'UniformOutput', false);
I would think a regular expression would also help you find your year, but without knowing what's in Y, I can't say for sure.
I would also think you may not even need the loop, but need a better description of the inputs.
采纳的回答
Guillaume
2016-10-12
编辑:Guillaume
2016-10-12
If I understood correctly:
%input:
%A: cell array of char arrays. wants to find the year in there.
% the year (always 4 digits) is preceded by a space and followed by more digits and a 'Z'
%outputs:
%duniqyear: the unique years encoded in A, numeric vector
%Asplit: Cell array of input grouped by year, corresponding to duniqyear. Asplit is thus a cell array of cell arrays of char arrays
yearstring = regexp(A, '(?<= )\d{4}(?=\d+Z )', 'match', 'once'); %extract year as string
assert(~any(cellfun(@isempty, yearstring)), 'Failed to find year in some string');
[duniqyear, ~, idx] = unique(str2double(yearstring)); %convert to numeric, get unique values and corresponding index in A
Asplit = accumarray(idx, 1:numel(A), [], @(indices) {A(indices)}); %distribute identical years in destination.
You ought to learn regular expressions as it's extremely useful to find and extract patterns from strings.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!