Import data from JSON to Matlab - matlab

Could you please help, I do not know how to get to data after opening JSON file in Matlab. When its open I get data in form of 3x1 cell containing 3 times 1x1 struct with also 19x1 struct data field and columns in first struct field: "data_time" "tooltip_value" "value"
part of JSON:
"data":[{"date_time":1597968882000,"tooltip_value":0.20513,"value":0.2051274641463},{"date_time":1598050973000,"tooltip_value":0.24814,"value":0.2481352158793},{"date_time":1598134408000,"tooltip_value":0.21524,"value":0.21524491588220002},{"date_time":1598405314000,"tooltip_value":0.21476,"value":0.2147630998212},{"date_time":1598549286000,"tooltip_value":0.19774,"value":0.1977396932195},{"date_time":1599127692000,"tooltip_value":0.22473,"value":0.22473225745569997},{"date_time":1600369794000,"tooltip_value":0.20077,"value":0.2007681102187},{"date_time":1601032111000,"tooltip_value":0.23335,"value":0.23335248258320002},{"date_time":1601198471000,"tooltip_value":0.22807,"value":0.22806941674040002},{"date_time":1601445642000,"tooltip_value":0.23584,"value":0.23584042110120002},{"date_time":1601620790000,"tooltip_value":0.21265,"value":0.21265419035879998},{"date_time":1601777380000,"tooltip_value":0.25022,"value":0.2502199889207},{"date_time":1601942767000,"tooltip_value":0.21143,"value":0.2114320344667},{"date_time":1602025213000,"tooltip_value":0.23081,"value":0.2308069067011},{"date_time":1602108652000,"tooltip_value":0.21114,"value":0.21114077299169998},{"date_time":1603184498000,"tooltip_value":0.21554,"value":0.2155385588332},{"date_time":1603350002000,"tooltip_value":0.2293,"value":0.229302053873},{"date_time":1603515737000,"tooltip_value":0.21503,"value":0.2150290288937},{"date_time":1603602042000,"tooltip_value":0.22225,"value":0.222248220073}],

To convert JSON into a Matlab structure use jsondecode.
s = '{"data":[{"date_time":1597968882000,"tooltip_value":0.20513,"value":0.2051274641463},{"date_time":1598050973000,"tooltip_value":0.24814,"value":0.2481352158793},{"date_time":1598134408000,"tooltip_value":0.21524,"value":0.21524491588220002}]}';
S = jsondecode(s);
Given part of your string, the result is a 1x1 structure S containing a 3x1 structure data.
To access the structure use dot indexing in the following way.
S.data(2).date_time
If part of your structure is a cell array, index it with {} brackets. Following your description this would be implemented like this.
S{3}.data(2).date_time
For futher reference I recommend reading struct documentation and cell array documentation.

Related

Synchronize timetables stored in a structure

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{:});

Trouble with looping function into structure index

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.

Matlab Array of structures: string not working

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};

how to present .mat data in matlab

I imported my data from excel to matlab and saved it as 't.mat'.I used function:
load('t.mat')
open('t.mat')
ans =
ndata: [62x8 double]
text: {63x9 cell}
alldata: {63x9 cell}
but it did not show the data.I want to see the column 5 of the data an plot it,so i wrote :
x=t.mat(:,5)
plot(x)
the error was :
??? Attempt to reference field of non-structure array.
please help me to present the data and plot it.
thanks
You can load your data into a structure and easily access them:
DataStruct = load('t.mat');
A = DataStruct.alldata; % Assign a variable to alldata.
plot(A{:,5});
Following on #Darthbit's comment, once you load the .mat file ,variables are available in the workspace, so you could use something like this:
load('t.mat');
plot(alldata{:,5})
't.mat' is just the name of the file where your data is stored. When you load a file, it loads the content into your workspace with the saved variable names, here ndata, text and alldata.
You then need to call the variables themselves to access the data:
load('t.mat')
x = ndata(:,5);
plot(x)
I haven't used Matlab in a while but I'm pretty sure you can't just say:
x=t.mat(:,5)
I think you have to store the t.mat into something. Here's a page that might be helpful:
http://www.mathworks.com/help/matlab/ref/load.html#btm3ohm-1

Matlab: Understanding a piece of code

I have a matlab code which is for printing a cell array to excel. The size of matrix is 50x13.
The row 1 is the column names.
Column 1 is dates and rest columns are numbers.
The dateformat being defined in the code is:
dFormat = struct;
dFormat.Style = struct( 'NumberFormat', '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(#_)' );
dFormat.Font = struct( 'Size', 8 );
Can someone please explain me what the dFormat.Style code means ?
Thanks
The first line creates an empty struct (struct with no fields) called dFormat. A structure can contain pretty much anything in one of its fields, including another structure. The second line adds a field called 'Style' to the dFormat struct and sets it equal to another struct with a field called 'NumberFormat'. The 'NumberFormat' field is set equal to that long string of characters. You now have a structure of structures. The third line is similar to the second.
Note that the first line isn't really necessary unless dFormat already exists and it needs to be "zeroed out" as dFormat.Style with create it implicitly. However, using the struct function can make code more readable in some cases as objects use a similar notation for access methods and properties. In other words, all of your code could be replaced with:
dFormat.Style.NumberFormat = '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(#_)';
dFormat.Font.Size = 8;
See this video from the MathWorks for more details and this list of helpful structure functions and examples.
#horchler already elaborated on structs, but I imagine you may actually be more interested in the content of this structs Style field.
In case you are solely interested in _(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(#_), that does not really look like something MATLAB related to me.
My best guess is that this code is used to later feed some other program, for examle to build an excel file.