Matlab calculating with Excel cells - matlab

I'm currently trying to calculate with values from a excel which i transformed from a txt file.
My code is:
clc, clear
data = readtable('C:\Users\Steffen\Desktop\Masterarbeit\splithopskinsbar_15_02_measurements\Ergebnisse_07_03_Probe_NEU_18x13_18x10_18x9\07_03_Probe_NEU_18x13_18x10_18x9\Steffen_Runkel_Neu003.txt');
writetable(data, 'Steffen_Runkel_Neu003.xlsx'); %erstellen der excel
A=readcell('Steffen_Runkel_Neu003.xlsx'); %read of excel
Count_lines=size(A); %count lines of matrix
CH_1=(A(2:Count_lines(1),2)); %definieren der WERTE
CH_2=(A(2:Count_lines(1),3)); %definieren der WERTE
time=(A(2:Count_lines(1),1)); %definieren der WERTE
CH_1_tranformed=str2double(CH_1);
CH_2_tranformed=str2double(CH_2);
time_transformed=str2double(time);
for i=1:Count_lines-1
CH_1_tranformed(i,1)=(CH_1_tranformed(i,1)/100000)*10^-6;
CH_2_tranformed(i,1)=(CH_2_tranformed(i,1)/100000)*10^-6;
end
y=CH_1_tranformed(1:Count_lines-1);
x=time(1:Count_lines-1);
plot(x,y);
the error which i get is
"Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the
table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
Error using plot
Invalid data argument.
Error in SHPB (line 26)
plot(x,y);"
so I'cant plot it...

Your readcell statement creates a cell array. You might try using readmatrix instead or use curly braces to to data out of cell array , e.g.,
CH_1=(A{2:Count_lines(1),2})

Related

add a table below a text in a file, Matlab fprinf fopen

I'm using Matlab to develop weather data.
I need to add few lines above the data.
I have a matrix with different data in columns. Let's say:
t=[2;3;6;8;9]; % temperature
v=[10;11;12;10;11]; % wind speed
r=[500;650;750;1000;750]; % solar radiation
table=array2table([t,v,r]);
writetable(table,'file.txt','WriteVariableNames',0);
location='rome';
year=2015;
line1=[location,'_',num2str(year)];
Now I need to add line1 above the numbers.
How can I use fopen and fprintf? With fprintf I just can indicate 'w' and 'a', that is to say overwrite or add below the text.
I thought I could do the contrary: first create a text file with line1 and than use the option 'a' to append the table below.
Unfortunately I can't do it: could you make me an example with my variables?
Thank you

loading txt files into matlab structure

I have a txt file below as shown in the attached figure:
a 0.15
ne 1e25
density 200
pulse_num 2
Is has n rows, 2 data on each row. The first data is a sting that contains the field name, and the second data contains the value. The two data is separated by a space. How do I load this txt file into a matlab structure? Basically I want something like:
whatIwant = struct('a', 0.15, 'ne', 1e25, 'density', 200, 'pulse_num', 2)
I only know how to load it to a table (using readtable), and I can convert the table to a cell, then to a structure. Problem is that I don't know how to append a structure. I don't want to input the field names in my code, so if I change the field names (or don't know the field names) the final structure will have the appropriate field names.
Or are there other simple ways to load it directly?
This can be done using:
fid = fopen('info.txt'); %Opening the text file
C = textscan(fid, '%s%s'); %Reading data
fclose(fid); %Closing the text file
%Converting numeric data stored as strings in a cell to numeric data using cellfun
s=cell2struct(cellfun(#str2double,C{2},'un',0),C{1},1); %Converting into a structure array
Read the documentation of fopen, textscan, fclose, cellfun and cell2struct for details.

Convert matlab fig into csv, txt or ascii

I have the m-file on generating all the graphs in .fig I need with a for loop, but I don't know how to extract the array data to csv, txt or ascii format (16-bit).
imagesc(x,y,C); %C is the data I want to extract
%m and n are variables created inside the for loop
I have tried dlmwrite and save, but I failed and could not fix the problem.
So I want to convert fig into csv, txt or ascii format instead.
filename_B=strcat(MM,'_profile'); %MM is a variable created inside the for loop
dlmwrite(filename_B.txt,squeeze(Data_time(:,m,n,:)),''); %Data_time is C
save(filename_B, squeeze(Data_time(:,m,n,:)),'-ascii','-double');
I have also tried csvwrite
filename_B=strcat(MM,'_profile');
csvwrite(filename_B.txt, squeeze(Data_time(:,m,n,:)));
but there is an error message, "??? Attempt to reference field of non-structure array."
It would be better if I could just extract the data directly to the desirable format without first producing fig then convert.
For confidential issue, I can't provide the whole script, but I will try my best to explain my problem.
Many thanks!

load txt dataset into matlab in a matrix structure

I need to read a txt dataset and do some analytics by matlab. the structure of the txt file is like this:
ID Genre AgeScale
1 M 20-26
2 F 18-25
So, I want to load this txt file and build a matrix. I was wondering if someone could help me with this. I used fopen function but it gives me a single array not a matrix with 3 columns.
MATLAB has an interactive data importer. Just type uiimport in the command window. It allows you to:
Name the variable based on heading as shown in your example. You can also manually change them.
Specify variable (column) type, e.g. numeric, cell array of strings, etc.
Auto generate an import script for next use (if desired)
If it works for you then congratulations, you don't need to waste hours to write an data import script :)
Function fopen only returns the file ID and not the content of the file. Once you open the file you use the file ID to read line by line and then parse each line with strsplit using space as the delimiter.
Here's one simple way of doing so:
fid = fopen('textfile.txt');
tline = fgetl(fid);
n = 1;
while ischar(tline)
data(n,:) = strsplit(tline(1:end-1),' ');
n=n+1;
tline = fgetl(fid);
end
fclose(fid);
Keep in mind that the matrix data is type string and not numeric, so if you want to use the numeric values of your dataset, you'll need to take a look at the functions str2num (str2double in newer versions) and strtok to split the AgeScale strings with delimiter '-'.

Iimport data in Matlab from Excel with string label

I am new in Matlab and I want to import an Excel file in matrix format. My columns are all numerical except the last column, which is a string and it's my labels. When I import my data set Matlab says that label column is unimportable and replaces it to zero! How can I do about it?
Please use following code:
[num, txt, raw] = xlsread(excelpath, sheetname, A00:C20)
Excep path, sheetname and A00:C20 are arbitrary. You can find table containing all items in specified cells in raw variable. num contains numeric values and txt contains string values.