I have a <850x1> cell called x. Each of the individual structures has a 'Tag' name and 'Data' cell with <7168x1 double> data values.
(i.e.
x{1,1}.Tag = 'Channel1', x{1,1}.Data= <7168x1 double>)
So, I want to go through the x cell, identify the structures with 'Channel1' Tag names and pull out that structure's data. Then, combine the data into a cell called Ch1. Here is my approach so far:
n=1:850
if x{n,1}.Tag == 'Channel1'
Ch1{:,n} = x{n,1}.Data;
end
However, this gives an error: Bad cell reference operation.
Any ideas what may be going wrong?
There are 2 issues here. First, your if statement will compare each entry in the string x{n,1}.Tag to each entry in the string 'Channel1'. If the dimensions are not the same, you will get an error. To fix this, you could use the string compare function, strcmp. The other issue is that you are assigning n to all values between 1 and 850 at once. This is the issue that is producing the actual error you are seeing. Instead, you want to step through each of these values one at a time with a for loop. I would suggest trying the following code:
for n=1:850
if strcmp(x{n,1}.Tag, 'Channel1')
Ch1{:,n} = x{n,1}.Data;
end
end
Related
I am dynamically storing data from different data recorders in timetables, nested in a structure DATA, such as DATA.Motor (timetable with motor data), DATA.Actuators (timetable with actuators data) and so on.
My objective is to have a function that synchronizes and merges these timetables so I can work with one big timetable.
I am trying to use synchronize to merge and synchronize those timetables:
fields = fieldnames(DATA);
TT = synchronize(DATA.(fields{1:end}));
but get the following error:
Expected one output from a curly brace or dot indexing expression, but there were 3 results.
This confuses me because DATA.(fields{1}) return the timetable of the first field name of the DATA structure.
Any thought on how I can solve this is greatly appreciated.
The problem here is that fields{1:end} is returning a "comma-separated list", and you're not allowed to use one of those as a struct dot-index expression. I.e. it's as if you tried the following, which is not legal:
DATA.('Motor','Actuators')
One way to fix this is to pull out the values from DATA into a cell array, and then you can use {:} indexing to generate the comma-separated list as input to synchronize, like this:
DATA = struct('Motor', timetable(datetime, rand), ...
'Actuators', timetable(datetime, rand));
DATA_c = struct2cell(DATA);
TT = synchronize(DATA_c{:});
I'm relatively new to matlab and would really appreciate any help.
Currently, I have a function (we'll call it readf) that reads in data from a single ascii file into a struct of multiple fields (we'll call it cdata).
names = cellstr(char('A','B','C','D','E','F','G'));
cdata = readf('filestring','dataNames',names);
The function works fine and gives me the correct output of a struct with these field names, with the value of each field name being a cell array of the corresponding data.
My task is to create a for loop that uses this readf function to read in a folder of these ascii files at once. I'm trying to work it so that the for loop creates a struct with an index of the different cdata structs. After trying a few different methods, I am stumped.
This is what I have so far.
files = struct2cell(dir('folderstring')); %creates a cell array of the names of the files withing the folder
for ii=length(files);
cdata(ii) = readf([folderstring,files(1,1:ii),names],'dataName',names);
end;
This is currently giving me the following error.
"Error using horzcat
Dimensions of matrices being concatenated are not consistent."
I am not sure what is wrong. How can I fix this code so i can read in all the data from a folder at once??? Is there a better and more efficient way to do this than making an index to this struct? Perhaps a cell array of different structures or even a structure of nested structures? Thanks!
Change:
for ii=length(files);
cdata(ii) = readf([folderstring,files(1,1:ii),names],'dataName',names);
end;
To:
for ii=1:length(files); % CHECK to make sure length(files) is giving you the right number
cdata(ii) = readf([folderstring,files{ii},names],'dataName',names);
end;
% CHECK files{ii}, with 1,2,3 etc. is giving you the correct file name.
I am reading an input from a file and importing it into my data to run in Matlab:
parts = strread(tline,'%s','delimiter',';')
employee(i).name = parts(1);
employee(i).salary= str2double(parts(2));
Then I try to print it out:
for i = 1:3
fprintf('salary: %.2f\n',employee(i).salary);
fprintf('employee name: %s\n',employee(i).name);
end
The salary prints with no problem. But the for the variable "name" it gives an error:
Error using fprintf
Function is not defined for 'cell' inputs.
fprintf('employee name: %s\n',employee(i).name);
I looked for some other examples:
access struct data (matlab)
How do I access structure fields dynamically?
Matlab Error: Function is not defined for 'cell' inputs
How do i define a structure in Matlab
But there is nothing to address this case, where only string is not working.
I have not explicitly declared the data as struct, i.e. inside the code there is nowhere the "struct" word is included, but Matlab apparently automatically understand it as an "Array of structures".
Any hints what might be missing here?
All comments are highly appreciated!
The issue is that employee(k).name is a cell (check with iscell(employee(1).name)) and the format string %s doesn't know how to handle that.
The reason that it is a cell is because strread returns a cell array. To grab an element from the result (parts), you want to use {} indexing which returns a string rather than () which will return a cell.
employee(i).name = parts{1};
Let me describe the question in this way. I have a .mat file, and if I open it, it contains a 1x10 struct data. In each data, it has a 1x5 struct (or field) called res. In res, it has a 1x1 struct (or field) called, let's say, foo. Thus, I have ixj copies of data(i).res(j).foo .
Is there anyway I can change the name of this foo? say I want all data(i).res(j).foo to become data(i).res(j).bar
I did search on the internet, and tried a few ways (add field and delete, create a temp field, use cell2field or fieldtofile, etc.) and all of them didn't work. The most frequent returned error is "Subscripted Assignment between dissimilar structures."
Please help, thanks in advance!
The safest way is probably by looping over data twice, the first pass creating a new field bar for each subfield like data(i).res(j).bar=data(i).res(j).foo, then the second pass deletes the old fields like data(i).res(j) = rmfield(data(i).res(j),'foo').
Thanks caoy and NotLikeThat. I finally came to an conclusion.
data2 = data
for i=1:10
for j = 1:5
data(i).res(j).bar = data2(i).res(j).foo;
end
data(i).res = rmfield(data(i).res, 'foo');
end
I probably need to removed i, j, and data2 after running this script.
I am trying to reference an element buried within a structure that I did not create (hence I don't know the exact way in which it was built).
Having loaded the structure, if I type:
dataFile.RECORDINGS.eye
I receive the following output:
ans =
2
ans =
2
Both of those variables will always be the same, but they could be at any time 1, 2 or 3. What I'd like to do is check with a switch statement which looks like this:
switch dataFile.RECORDINGS.eye
case {1, 2}
% action A
case 3
% action B
end
Of course, the above throws up an error because 'case' cannot check whether dataFile.RECORDINGS.eye contains a given value since there are two elements stored under that address. So, my question is: how do I reference just one of the elements? I thought it would be as simple as replacing the first line with:
switch dataFile.RECORDINGS.eye(1)
...But, this gives the error:
??? Field reference for multiple structure elements that is followed by more reference blocks is an error.
Similarly, I can't access the element like this:
switch dataFile.RECORDINGS.eye.1
...As I get the following error:
??? Dot name reference on non-scalar structure.
If the values are really always the same, you can try the following to get a scalar that can be used in the switch command:
unique([dataFile.RECORDINGS.eye])
By the way, did you try to index RECORDINGS, i.e.,
dataFile.RECORDINGS(1).eye
dataFile.RECORDINGS(2).eye
Perhaps instead of eye having multiple elements, you have multiple elements of RECORDINGS that each have a single value of eye? You might want dataFile.RECORDINGS(1).eye or dataFile.RECORDINGS(2).eye.