how to Vectorize this for loop
1 次查看(过去 30 天)
显示 更早的评论
Hi everyone i have wrote this code and i want to vectorize my code so it run faster here is the part of code i'm talking about
fileID = fopen('CANDec(day_1_9).txt','r');
text_data = textscan(fileID,'%s %s %s %s %s %s %s %s',1000,'delimiter',';');
hex=text_data(:,4);
signal_name=text_data(:,7);
decimal_wert=text_data(:,8);
for i =1:length(text_data{1,1})
can.(signal_name{1,1}{i,1}).hex{i,1}=hex{1,1}{i,1};
can.(signal_name{1,1}{i,1}).decimal_wert{i,1}=decimal_wert{1,1}{i,1};
end
hex , signal_name and decimal_wert are cells that i read from a .txt file (CANDec(day_1_9)). it has actually many million lines . it's means there is millions of signal_name but here i read just 1000 lines.
now i'm building a structure can and as the name for substructures i use signal_name dynamically. if you go in can-structure you see all signal_name as substructure and in these substruct you find again 2 substruct with the names hex and decimal_wer.
i hope you understand the code . so my goal is to vectorize this for loop . when i remove the for and vectorize it simply I will get this error :
Expected one output from a curly brace or dot indexing expression, but there were 1000 results.
i =1:length(text_data{1,1})
can.(signal_name{1,1}{i,1}).hex{i,1}=hex{1,1}{i,1};
can.(signal_name{1,1}{i,1}).decimal_wert{i,1}=decimal_wert{1,1}{i,1};
i hope you can help me with that.
Thank you
0 个评论
采纳的回答
Stephen23
2020-2-13
编辑:Stephen23
2020-2-13
Try doing something like this:
[fid,msg] = fopen('CANDec(day_1_9).txt','rt');
assert(fid>=3,msg) % good practice to check the FID.
C = textscan(fileID,'%s%s%s%s%s%s%s%s',1000,'delimiter',';');
fclose(fid);
% Simpler to get the CONTENTS of the cell array:
hx = C{4}; % hex
sn = C{7}; % signal name
dw = C{8}; % decimal wert
I strongly recommend that you do not use nested structures, but instead store the signal name as data in its own right in a non-scalar structure, then your task is trivial:
can = struct('signalName',sn, 'hex',hx, 'decimalWert',dw)
And you can access the data in can using some very convenient sytaxes:
5 个评论
Stephen23
2020-2-14
编辑:Stephen23
2020-2-14
sn = { 'B_UF'; 'B_IN'; 'B_UF'; 'B_UF'; 'B_STMAX'; 'B_STMAX'};
hx = { '374C'; '025A'; '00'; 'BB'; 'AA'; 'F9'};
dw = {'704.17';'29.94'; '0'; '1234'; '0'; '249.0'};
[fn,~,ix] = unique(sn);
Using nested structures:
can = struct();
for k = 1:max(ix)
can.(fn{k}).hex = hx(k==ix);
can.(fn{k}).dWt = dw(k==ix);
end
Checking:
>> can.B_UF.hex
ans =
'374C'
'00'
'BB'
Or with a non-scalar structure:
can = struct();
for k = 1:max(ix)
can(k).name = fn{k};
can(k).hex = hx(k==ix);
can(k).dWt = dw(k==ix);
end
Checking:
>> can(3).name
ans =
B_UF
>> can(3).hex
ans =
'374C'
'00'
'BB'
I strongly recommend that you use a non-scalar structure. As well as the ease of access, consider what your code would do if one of the signal_names is not a valid fieldname.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!