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

 Respuesta aceptada

Stephen23
Stephen23 el 13 de Feb. de 2020
Editada: Stephen23 el 13 de Feb. de 2020
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 comentarios

Milad Ghobadi
Milad Ghobadi el 14 de Feb. de 2020
Editada: Milad Ghobadi el 14 de Feb. de 2020
Thank you Stephen,
The reasone that I use these nested structures is that, the signal_names in this text-data will be repeated several time and every time with a new hex-value so i wanted to gather all this hex and decimal value under the name of corresponding signal-name in order to see them as I open a signal and thats the reasone that i used this dynamic naming ( can.(signal_name{i}) ) . It seems first like this:
can.(signal_1).hex(1)=AB
can.(signal_2).hex(1)=A2
can.(signal_3).hex(1)=C4
can.(signal_2).hex(2)=FE51
can.(signal_1).hex(2)=1DFA
can.(signal_2).hex(3)=1235
and after gathering all same signal together it should be like below:
can.signal_1.hex= AB , 1DFA
can.signal_2.hex=A2 , 1DFA , 1235
can.signal_3.hex=C4
I hope i could find a solution for this
Many Thanks
Stephen23
Stephen23 el 14 de Feb. de 2020
Editada: Stephen23 el 14 de Feb. de 2020
"I hope i could find a solution for this "
You would need to use loop, which is why accessing data in nested structures is usually not very convenient.
But what you show does not require nested structures, it would be much simpler to generate and access using a non-scalar structure:
can(1).hex = {'AB','1DFA'};
can(1).name = 'signal_1';
can(2).hex = {'A2', '1DFA', '1235'};
can(2).name = 'signal_2';
...
Milad Ghobadi
Milad Ghobadi el 14 de Feb. de 2020
Editada: Milad Ghobadi el 14 de Feb. de 2020
I cann't get it now can looks like this:
'B_UF' '374C' '704.17'
'B_IN' '025A' '29.94'
'B_UF' '00' '0'
'B_UF' 'BB' '124'
'B_STMAX' 'AA' '0'
'B_STMAX' 'F9' '249.0'
but it should be as shown as below:
'B_UF' ['374C' , '00' , 'BB'] [ '704.17' ,'0' , '124']
B_IN' '025A' '29.94'
B_STMAX' ['AA' , 'F9' ] ['0' , '249.0' ]
here is a sample of text-data:
1567081890.074777;2F357C0000943900;8000018A;352F;2-BYTE;Stat_N_BMU_10;BMU_UF_10;677.259795
1567081890.074777;2F357C0000943900;8000018A;007C;2-BYTE;Stat_N_BMU_10;BMU_IN_10;1.780835
1567081890.074777;2F357C0000943900;8000018A;3994;2-BYTE;Stat_N_BMU_10;BMU_UN_10;674.743694
Stephen23
Stephen23 el 14 de Feb. de 2020
Editada: Stephen23 el 14 de Feb. de 2020
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.
Milad Ghobadi
Milad Ghobadi el 17 de Feb. de 2020
Oh of coure old fashioned unique I almost forget about it . Great Thanks alot

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Performance en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 13 de Feb. de 2020

Comentada:

el 17 de Feb. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by