PARFOR loop can not run due to the way variable "M" is used.

1 次查看(过去 30 天)
Hello,
I'm wondering what I can do to variable "M" to make it PARFOR - friendly while keeping it's function.
Any help is appricated - Thanks in advance.
Please find the code below:
clc
clear all
path = 'SW_OPER_MAGA_HR_1B_20170214T000000_20170214T235959_0505_MDR_MAG_HR.cdf';
info = cdfinfo(path) ;
vars=info.Variables ;
data=cdfread(path,'Variables',{'Latitude'});
timestamp=(cdfread(path,'Variables',{'Timestamp'}));
parfor i=1:length(timestamp);
fprintf('processing %d of %d time step one',i,length(timestamp));
datenum(i) = todatenum(timestamp{i});
[year(i), month(i),day(i) ,hh(i),mm(i),ss(i)]=datevec( todatenum(timestamp{i}));
fprintf(' - Time step one calc done \n');
end
parfor i=1:length(mm);
fprintf('processing %d of %d time step two',i,length(mm));
time(i) = hh(i) + mm(i)/60 +(ss(i)/3600);
fprintf(' - Time step two calc done \n');
end
UT = hh+(mm/60)+(ss/3600);
data1 = cell2mat(data);
lat = data1(:,1);
fprintf('start of find...');
f0 = find(UT>0&UT<24);
fprintf(' - find done\n');
f=f0;
f1 = horzcat(f,f(end)+1:f(end));
fprintf(' - f1 done\n');
fprintf('start of cell2mat data1...');
data1=cell2mat(cdfread(path,'Variables',{'Latitude','Longitude','Radius'}));
fprintf(' - cell2mat data1 done\n');
parfor i=1:length(f1);
fprintf('processing %d of %d variables',i,length(f1));
Alt(i) = (data1(f1(i),3)/1000);
fprintf(' - Variables calc done \n');
end
parfor i=1:length(f1);
fprintf('processing %d of %d ms',i,length(f1));
M(i,1) = data1(f1(i),1);
M(i,2) = data1(f1(i),2);
M(i,3) = Alt(i);
M(i,4) = datenum(f1(i));
fprintf(' - ms calc done \n');
end

采纳的回答

Raymond Norris
Raymond Norris 2022-2-4
Collapse
M(i,1) = data1(f1(i),1);
M(i,2) = data1(f1(i),2);
M(i,3) = Alt(i);
M(i,4) = datenum(f1(i));
to
M(i,:) = [data1(f1(i),1) data1(f1(i),2) Alt(i) datenum(f1(i))];
By the way, there's a MATLAB function, datenum. You might consider using a different variable name.
  2 个评论
Pouya
Pouya 2022-2-5
编辑:Pouya 2022-2-5
Thank you Raymond, it worked like a charm!
Can you explain your point on "datenum" a bit further. I didn't quite get it.
Raymond Norris
Raymond Norris 2022-2-8
In the call to
datenum(i) = todatenum(timestamp{i});
you are overwriting the MATLAB function, datenum.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by