How to save data in multiple sheets with xlswrite?

1 回表示 (過去 30 日間)
Linus Dock
Linus Dock 2016 年 10 月 11 日
コメント済み: Linus Dock 2016 年 10 月 12 日
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
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.
Linus Dock
Linus Dock 2016 年 10 月 12 日
Ok sorry for the messy code. Here is my Y:
Y=num2str(duniqyear)
Y =
2008
2009
2010
What I'm trying to do with the code:
X=regexprep(Y(f,:),Y(f,:),sprintf('%s$0','Z'))
Is to prepend the year with 'Z' as you mentioned. Thereafter I would like to find 'Z2008' in A. This is clearly not working, but what I really want to do is to find every word or group of the form: 201002282350Z in A.
Is i possible to combine for example '2009--------Z' where --- is mmddHHMM or is there Another way of doing this?
Thank you for your help!

サインインしてコメントする。

採用された回答

Guillaume
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.
  1 件のコメント
Linus Dock
Linus Dock 2016 年 10 月 12 日
This worked out! Awesome! Very nice, now I have my data neatly packaged and separated year-by-year in sheets in excel. Thank you so much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by