so I have a file, data.dat that I have imported into my Matlab:
myData=importdata('data.dat');
and it has the layout of showing time, followed by 2 lots of 3 values eg.
0.000
0.744
0.313
0.982
-2.19
0.383
0.324
0.313
-2.09
2.883
0.827
0.875
-0.293
1.11
Is it possible for me to be able to assign the first 3 values of every 'set' to a variable so I am able to plot it?
(e.g. I want 0.744, 0.313, 0.982 and -2.09,2.883,0.827 assigned to a variable - or even 3 variables (as these represent values in the(x,y,z)-plane)
please could someone offer some help?
Thank you!
If you are completely confident about the structure of the data, you can just hard code a little for loop to construct a matrix out of the dataset:
>> x=[0, 0.744, 0.313, 0.982, -2.19, 0.383, 0.324, 0.313, -2.09, 2.883, 0.827, 0.875, -0.293, 1.11];
>> for n=1:length(x)/7
lots(n,:) = x((n-1)*7+1:n*7);
end
Then you can just index the values you want from the rows of values you are interested in. For example:
>> figure
>> plot(lots(1,2:5))
Related
Hello I'm building a exe from a simulink model and in order to do that I pass the inputs to it through a .mat file.
My question is, since in my model is present a "for each" block, how can I store the data in the .mat file? Normally (without the for each block) I would store the input as a constant vector in the workspace (see the upper part of the simulink model) and it will handle automatically how to pass the data during the simulation time. But in my case, since I want to export as .exe and pass the input programmatically, I need the input as .mat file and the presence of the "for each" block screw up the building the vector time (since is unclear how to combine time vector with data vector inside the .mat file because is unclear to simulink which data take at a given simulation time.
Thank you for any help!
It's not really clear what the specific problem you are having is.
In your upper diagram, the model will run when you have a variable
>> input = 1:3;
If you turn on Display->Signals & Ports->Signal Dimensions then you'll see that the signal coming out of the Constant block has a dimension of 3.
For the lower diagram, create a variable in MATLAB, that since it will be used in a From File block, must adhere to the specifications required for that block, which means the first row is a time vector, so
>> t = [0 10]
t =
0 10
>> u = [1 1;2 2;3 3]
u =
1 1
2 2
3 3
>> tu = [t;u]
tu =
0 10
1 1
2 2
3 3
And then save this variable to your file,
>> save input tu
Now the signal coming out of the From File block will also be of dimension 3.
Change the values of t and u to suit your specific problem.
I have a 17G netcdf file that I am trying to use for analysis. Each variable in the netcdf file is set up like: variable(x,y,z,time). I would like to read in and analyze the variables one 'time' at a time for analysis in Matlab. In other words, I want to use all x, y, and z points at one time. In the past I have had smaller files so reading in a variable has been set up like
fid=netcdf.open('filename/location','NC_NOWRITE');
var_id=netcdf.inqVarID(fid,'varname');
var=netcdf.getVar(fid,var_id);
Is it possible to read in the variables using one time step when the variable is read in? (Incorrect syntax) It'd essentially look like
var=netcdf.getVar(fid,var_id,[:,:,:,time_index]);
Yes, the matlab netcdf command supports this, almost the way you wrote it:
data = netcdf.getVar(fid,var_id,var_index,var_length)
See the matlab documentation for more information. You can also use high-level matlab commands instead of the netCDF library functions.
For example, if varname is a 100x4 array, you could get row 7 by using:
% read 4 columns from 1 row of data starting at row 7, column 1
v = ncread('filename/location','varname',[7 1],[1 4]);
or a four-dimensional array, as in the question:
% read all data from dim. 1-3 at dim 4 = 27
v = ncread('filename/location','varname',[1 1 1 27],[Inf Inf Inf 1]);
I have started working with Matlab recently. I only know basic matlab operations.So, i am struggling with the following problem. I would appreciate someone's kind help.
I have a text file in the format below whose record is expected to update each time according to the CoorsID. I need to take account of each row one after another (maintaing the CoorsID) and multiply the timed value with 10 for walk, 12 for urbanPuTLLLL,13 for urbanPuTDDDD and 14 for urbanPuTBus, then save in new matrices according to walk and other options until the sum of the Distance value become=18917.70763.
I did that bringing the dataset as a matrix in the matlab which was fairly easy. But, i cannot even figure out any clue to do this by line by line calcualtion. Advanced Thanks for your expert suggestion.
-saniul
###NumbersID CoorsID Distance Timed Trailinformation###
###33 0 0 0 walk###
###33 1 1.881326228 0.152869117 walk####
###33 2 2.861352906 2.211478513 urbanPuTLLLL####
###33 3 2.894228468 2.381984857 urbanPuTLLLL####
###33 4 3.77420476 3.807208533 urbanPuTDDDD####
###33 5 3.893702709 3.911981757 urbanPuTDDDD####
###33 6 3.934894721 4.022707315 urbanPuTBus####
###33 7 4.230054787 4.131792708 urbanPuTBus####
First I put the data you show in your question in to a file I named test.m. Then this code reads it in to Matlab:
fid = fopen('test.m','r');
L=fgetl(fid); % gets first line "header"
L=fgetl(fid); % gets first data line
I=1;
while ~isnumeric(L) && L(1)~=-1 % fgetl(fid) returns -1 at end of file
A(I,1:5) = textscan(L,'###%d %d %f %f %[^#]');
L = fgetl(fid);
I=I+1;
end
Use doc fgetl to open a help window to read about fgetl, and you can then read about the other commands.
A is a cell array with all the values converted in it. Do doc cell to read about these.
Look in the doc for textscan to read about the input formatting I did. ###%d meant "skip three # then convert the rest to an integer." %[^#] meant "read everything EXCEPT #."
From there, you can access the different elements of A, so A{3,4} is the number stored in the 3rd row, 4th column of the cell array named A, it is 2.211478513
You go ahead and figure out how to do the multiplies and the addressing and the decisions when to stop adding things.
I'm working with a really large number of matrix, just different in a single number (meanT0601, meanT0602,meanT0603,... and so on).
I know how to process a sequence of files by using a for loop and %d
but I'm unable to do something similar with matrices.
Any idea about that?? Although it will be a great lost of time I'm considering type every different matrix name to process them.
Thanks in advance
I dont know the command importdata (maybe my matlab version is too old), but if I'm using a combination of dlmread and dlmwrite it works fine.
A = [1 2 3 4; 5 6 7 8];
dlmwrite('file.txt',A);
B{1} = dlmread('file.txt');
Dlmread and dkmwrite save in a csv format. In the End everything is in the cell array B. If you have to use the importdata function, please provide the source or documentation
For some reason, the hdf5write method in MATLAB is automatically converting my row vectors to column vectors when I re-read them:
>> hdf5write('/tmp/data.h5','/data',rand(1,10));
>> size(hdf5read('/tmp/data.h5','/data'))
ans =
10 1
However, for a row vector in the third dimension, it comes back just fine:
>> hdf5write('/tmp/data.h5','/data',rand(1,1,10));
>> size(hdf5read('/tmp/data.h5','/data'))
ans =
1 1 10
How can I get hdf5write to do the right thing for row vectors? They should be coming back as 1 x 10, not 10 x 1.
edit the problem is slightly more complicated because I am using c-based mex to actually read the data later, instead of hdf5read. Moreover, the problem really is in hdf5write, and this is visible in the hdf5 files themselves:
>> hdf5write('/tmp/data.h5','/data',randn(1,10));
>> ! h5ls /tmp/data.h5
data Dataset {10}
that is, the data is saved as a 1-dimensional array in the hdf5 file. For comparison, I try the same thing with an actual 2-d matrix (to show what it looks like), a 1-d column vector, a 1-d vector along the third dimension, and, for kicks, try the V71Dimensions trick which is in the help for both hdf5read and hdf5write:
>> hdf5write('/tmp/data.h5','/data',randn(10,1)); %1-d col vector
>> ! h5ls /tmp/data.h5
data Dataset {10}
>> hdf5write('/tmp/data.h5','/data',randn(1,1,10)); %1-d vector along 3rd dim; annoying
>> ! h5ls /tmp/data.h5
data Dataset {10, 1, 1}
>> hdf5write('/tmp/data.h5','/data',randn(2,5)); %2-d matrix. notice the reversal in dim order
>> ! h5ls /tmp/data.h5
data Dataset {5, 2}
>> hdf5write('/tmp/data.h5','/data',randn(1,10),'V71Dimensions',true); %1-d row; option does not help
>> ! h5ls /tmp/data.h5
data Dataset {10}
So, the problem does seem to be in hdf5write. The 'V71Dimensions' flag does not help: the resultant hdf5 file is still a Dataset {10} instead of a Dataset {10,1}.
It's the reading that's an issue. From the help
[...] = hdf5read(..., 'V71Dimensions',
BOOL) specifies whether to change the
majority of data sets read from the
file. If BOOL is true, hdf5read
permutes the first two dimensions of
the data set, as it did in previous
releases (MATLAB 7.1 [R14SP3] and
earlier). This behavior was intended
to account for the difference in how
HDF5 and MATLAB express array
dimensions. HDF5 describes data set
dimensions in row-major order; MATLAB
stores data in column-major order.
However, permuting these dimensions
may not correctly reflect the intent
of the data and may invalidate
metadata. When BOOL is false (the
default), the data dimensions
correctly reflect the data ordering as
it is written in the file — each
dimension in the output variable
matches the same dimension in the
file.
Thus:
hdf5write('/tmp/data.h5','/data',rand(1,10));
size(hdf5read('/tmp/data.h5','/data','V71Dimensions',true))
ans =
1 10
I'm affraid for this you will have to use the low-level HDF5 API of Matlab.
In Matlab, the low-level API is available using for instance H5.open(...), H5D.write(...) and so on. The names correspond exactly to those of the C library (see the HDF5 doc). However there is a slight difference in the arguments they take, but the matlab help function will tell you everything you need to know...
The good news is that the Matlab version of the API is still less verbose than the C version. For instance, you don't have to close manually the datatypes, dataspaces, etc. since Matlab closes them for you when the variables go out of scope.