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
Related
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.
I have a huge arrays in matlab that I am concatenating into a table and then I am dumping that table into a .csv file. Shown below:
% Create manipulated results with noise table
busResultNoise = array2table(busResult_arr); %This command converts the array into a table
% Horizontally concatenate timestamp table and bus data measurements with
% noise table
busResultNoise = horzcat(timeStamp_bus_tab,busResultNoise); %adds the timestamps array
% Assign same variable to busResultNoise as in busResult table
busResultNoise.Properties.VariableNames= busResult.Properties.VariableNames;%adds column names
%Export csv
writetable(busResultNoise,'BusDataWithNoise.csv');
This works great especially if the table/array is small. Is there a faster way to write to .csv file (or may be a text file with commas in it)? I am not required to form a table as shown above but I am doing so because I thought adding timestamp column and adding column names is easier. Please suggest if there is a faster way to do it because this code chokes if the array/table is really large.
In Excel you can use the "filter" function to find certain words in your columns. I want to do this in Matlab over the entire table.
Using the Matlab example-table "patients.dat" as example; my first idea was to use:
patients.Gender=={'Female'}
which does not work.
strcmp(patients.Gender,{'Female'})
workd only in one column ("Gender").
My problem: I have a table with different words say 'A','B','bananas','apples',.... spread out in an arbitrary manner in the columns of the table. I only want the rows that contain, say, 'A' and 'B'.
It is strange I did not find this in matlab "help" because it seems basic. I looked in stackedO but did not find an answer there either.
A table in Matlab can be seen as an extended cell array. For example it additionally allows to name columns.
However in your case you want to search in the whole cell array and do not care for any extra functionality of a table. Therefore convert it with table2cell.
Then you want to search for certain words. You could use a regexp but in the examples you mention strcmp also is sufficient. Both work right away on cell arrays.
Finally you only need to find the rows of the logical search matrix.
Here the example that gets you the rows of all patients that are 'Male' and in 'Excellent' conditions from the Matlab example data set:
patients = readtable('patients.dat');
patients_as_cellarray = table2cell(patients);
rows_male = any(strcmp(patients_as_cellarray, 'Male'), 2); % is 'Male' on any column for a specific row
rows_excellent = any(strcmp(patients_as_cellarray, 'Excellent'), 2); % is 'Excellent' on any column for a specific row
rows = rows_male & rows_excellent; % logical combination
patients(rows, :)
which indeed prints out only male patients in excellent condition.
Here is a simpler, more elegant syntax for you:
matches = ((patients.Gender =='Female') & (patients.Age > 26));
subtable_of_matches = patients(matches,:);
% alternatively, you can select only the columns you want to appear,
% and their order, in the new subtable.
subtable_of_matches = patients(matches,{'Name','Age','Special_Data'});
Please note that in this example, you need to make sure that patients.Gender is a categorical type. You can use categorical(variable) to convert the variable to a categorical, and reassign it to the table variable like so:
patients.Gender = categorical(patiens.Gender);
Here is a reference for you: https://www.mathworks.com/matlabcentral/answers/339274-how-to-filter-data-from-table-using-multiple-strings
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.
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 !!