Assign type to column - matlab

I've imported the data of daily returns into a MatLab matrix using the importdata function:
19990104 0.0269360000000000
19990105 0.0170490000000000
...
I need to create a histogram of data in the second column. What's the best way to create a histogram of data in the second column? Do I need to assign types to columns separately?
Thank you for any suggestions.

Related

MATLAB for loop through columns to plot multiple lines

I need your help again :). I'm trying to plot multiple lines for a very large dataset. To start easier, I divided the dataset to get a TABLE in Matlab that contains 6 columns, with the first column representing the date that I want on my x-axis. Now I want to plot the other columns (and in the original file are a lot more than 6 columns) on the y axis, using a for loop. I tried the following, with no success:
hold on
for i=2:1:6
plot(Doldenstock(:,1), Doldenstock(:,i));
end
hold off
As I understand this, this code would do exactly what I want for columns 2,3,4,5,6. However, I always get the same error code:
Error using tabular/plot
Too many input arguments.
Error in Plotting_bogeo (line 6)
plot(Doldenstock(:,1), Doldenstock(:,i));
Now, I don't know if maybe for loops like this don't work for tabes but only for arrays?
Thanks for your help in advance!
Cheers,
Tamara
The function plot(x) expect x to be a scalar, a vector, or a matrix. But in your case the input is a table, because accessing a table with parentheses return a table, which is not supported.
If you read the doc "how to access data in a table" you will figure out that you need to use curly brace {} to extract the raw data (in your case a 1D matrix).
So use:
plot(T{:,1},T{:,2})

How do you multiply all elements of a table in Matlab?

suppose I have a table named dataset(xls table)
I want to Multiply all element by 3.
How can I do that?
I'm going to assume that you mean that you have imported an xls file into Matlab and it is now in a table that you want to perform a mathematical operation on. Since you can't do that to a table, you need to first convert it to an array, and then you can perform operations on it.
Assuming that all your variables are numeric, you can do that using:
table2array(dataset).*3;
If you want to save it back in table format, try:
dataset{:,:} = dataset{:,:}.*3

Choose and plot lines in a structure

Im having an issue specifying particular lines in a structure and plotting them.
I use tblread to take data from a file:
table_data = tdfread(table,',');
The table has both numerical and text data, looks a bit like this:
protocol,num_nodes,scale_physical,density,trace,reliability
etx,50,4.7045454546,4.94,heavy,72.7
nh,50,3.8275862069,4.96,heavy,64.27
rtt,50,4.5454545455,5.12,heavy,50.44
etx,50,3.8275862069,4.88,light,93.33
nh,50,4.7272727273,4.94,light,82.45
The resultant data type is a scalar structure called table_data. I can plot each column against each other using:
scatter(table_data.scale, table_data.reliability)
What I would like to achieve is to plot specific elements in the columns defined by a value in another column. Eg. plot the scale vs reliability where protocol = "nh".
Essentially I would like to achieve this end result like:
scatter(table_data.scale(table_data.protocol='nh'), table_data.reliability(table_data.protocol='nh'),'r')
hold on
scatter(table_data.scale(table_data.protocol='rtt'),table_data.reliability(table_data.protocol='rtt'),'b')
To differentiate the two types of points on the plot.
Is there any way of achieving this in a manor alluded to above.
Thanks.
EDIT:
solution is as follows:
scatter(table_data.scale(table_data.protocol=='n'),table_data.reliability(table_data.protocol=='n'), 'r')
hold on
scatter(table_data.scale(table_data.protocol=='e'),table_data.reliability(table_data.protocol=='e'), 'b')
In the structure The text is held as array or characters rather than a cell. table_data.protocol then points only references the first character in the array.
I think you want to replace the single equals in:
scatter(table_data.scale(table_data.protocol='nh'), table_data.reliability(table_data.protocol='nh'),'r')
to be a double equals like:
scatter(table_data.scale(table_data.protocol=='nh'), table_data.reliability(table_data.protocol=='nh'),'r')
i.e. you want a comparison operation instead of an assignment operation.

dlmwrite for specific rows and columns in matlab

I have 2D data for 10x10 matrices, and it looks like this
here is the table
However, data is updating and append every dt calculation, therefore I would like to reorganize and write it for specific columns , you may see this table in link
I use normally those codes to write
t=t+dt;
if ss==2000
dlmwrite('d:\Model_Results_Theta.txt', Tnew,'-append');
ss=0;
end
Could you recommend me any different way to organize the data based on the specific rows and columns using the matlab codes? Thanks in advance !!

Matlab Importdata

I'm currently writing a piece of code which is supposed to import text files using importdata and count the number of columns, i thought the cols() function would suffice for this, but it seems that all the imported data is stored as a double, meaning I can't perform this operation.
M=importdata('title');
numcols=cols(M.data);
Am I doing something wrong? I thought the data fromthe text file would be stored in a matrix/array?
Cols is a specific function for use in the database toolbox.
You probably just want to use the size function. EG:
size(M.data,2); %Returns number of columns
FYI
size(M.data, 1); %Returns number of rows.