i want to concatenate two variable but getting an error..."Names of fields in structure arrays being concatenated do not match. Concatenation of structure arrays requires that these arrays have the same set of fields"

3 visualizaciones (últimos 30 días)
I want to concatenate two variables. first i loaded my data which is a matrix, to both variables then store particular part of variables in another variable and then concatenate them. Here is my code.
t=load('2');
v=load('3');
T.a =t(:,2:size(t,2));
c=v(:,2:size(v,2));
ans=cat(2,T.a,c);
Here '2','3' are my files.
but i am getting error...
Error using cat Names of fields in structure arrays being concatenated do not match. Concatenation of structure arrays requires that these arrays have the same set of fields.
Error in Untitled3 (line 5) ans=cat(2,T.a,c); i think becoz when i load data it stores in variables with different field names.
Plz provide some solution . i have to use it
  2 comentarios
KSSV
KSSV el 7 de Dic. de 2016
Editada: KSSV el 7 de Dic. de 2016
What data the files have? Why don't you attach files if possibles?
MAYANK RATHORE
MAYANK RATHORE el 9 de Dic. de 2016
Editada: MAYANK RATHORE el 9 de Dic. de 2016
Sir my data is nothing but simply a matrix ,suppose a 3*3 matrix.As i am loading it to a variable it comes with a field value.And whenever i have to copy that data i have to mention the field value ,which is given by matlab.

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 7 de Dic. de 2016
A bold guess:
File2Data = load('2');
t = File2Data.t;
File3Data = load('3');
v = File3Data.v;
T.a = t(:, 2:size(t,2));
c = v(:, 2:size(v,2));
Reply = cat(2, T.a, c);
load replies a struct, not the values directly. You can check this manually.
  3 comentarios
Jan
Jan el 9 de Dic. de 2016
The field names are defined by the names of the variables during saving. So they are not "random". But if you do not know the names:
Data = load(FileName);
Fields = fieldnames(Data);
for k = 1:length(Fields)
disp(Data.(Fields{k}))
end
MAYANK RATHORE
MAYANK RATHORE el 9 de Dic. de 2016
Sir ur example of display field name is very nice but as u say in first line "fieldnames are defined by the names of variable" .Suppose I am loading my data in MYDATA variable ,but it stored with field name 'a' and whenever i want to access the data i have to write MYDATA.a .This is the problem this fieldname is given my matlab. And also, whenever i stored it as MYDATA.t it will store as MYDATA.T.a .

Iniciar sesión para comentar.

Categorías

Más información sobre Structures en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by