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"

2 views (last 30 days)
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 Comments
MAYANK RATHORE
MAYANK RATHORE on 9 Dec 2016
Edited: MAYANK RATHORE on 9 Dec 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.

Sign in to comment.

Answers (1)

Jan
Jan on 7 Dec 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 Comments
Jan
Jan on 9 Dec 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 on 9 Dec 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 .

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!