hey all, I'm having trouble tying to print the second column of this .dat file - matlab

I cannot seem to access the second column with this line of code. I am very new to Matlab and honestly feel like I have tried everything. I would really appreciate some help. here is my code:
A1data=fopen("specimenA1.dat",'r');
%ForceA1= (textscan(A1data,'%s','Delimiter','\n','Whitespace',''));
% importdata("specimenA1.dat");
ForceA1 = mat2cell(textscan(A1data,'%*s %n %*s %*s','Delimiter',),1);
celldisp(ForceA1)

Related

Reading data from .txt file into Matlab

I have been trying in vain for days to do one seemingly simple thing--I want to read data from a .txt file that looks like this:
0.221351321
0.151351321
0.235165165
8.2254546 E-7
into Matlab. I've been able to load the data in the .txt file as a column vector using the fscanf command, like so:
U=fscanf(FileID, '%e')
provided that I go through the file first and remove the space before the 'E' wherever scientific notation occurs in the data set.
Since I have to generate a large number of such sets, it would be impractical to have to do a search-and-replace for every .txt file.
Is there a way for matlab to read the data as it appears, as in the above example (with the space preceding 'E'), and put it into a column vector?
For anyone who knows PARI-GP, an alternate fix would be to have the output devoid of spaces in the first place--but so far I haven't found a way to erase the space before 'E' in scientific notation, and I can't predict if a number in scientific notation will appear or not in the data set.
Thank you!
Thank you all for your help, I have found a solution. There is a way to eliminate the space from PARI-GP, so that the output .txt file has no spaces to begin with. I had the output set to "prettymatrix". One needs to enter the following:
? \o{0}
to change the output to "Raw," which eliminates the space before the "E" in scientific notation.
Thanks again for your help.
A simple way, may not be the best, is to read line by line, remove the space and convert back to floating point number.
For example,
x = []
tline = fgetl(FileID);
while ischar(tline)
x = [x str2num(tline(find(~isspace(tline))))]
tline = fgetl(FileID);
end
One liner:
data = str2double(strsplit(strrep(fileread('filename.txt'),' ',''), '\n'));
strrep removes all the spaces, strsplit takes each line as a separate string, and str2double coverts the strings to numbers.

read tow Column txt file in specific directory and import to matlab

I have two Column txt file every Column contain the speed on dc motor. I want to plot every Column with the time and compaire the two curves.
I tried this code, but not working:
fid = fopen('C:\Users\Hussam Yonis\Desktop\recive.txt','r');
KK = fscanf(fid,'%f %f',[2,50]);
t=0:0.05:0.05*length(a(:,1))-0.05;
plot(t,fid(:,1),'b',t,fid(:,2),'r')
fid is just a pointer corresponding to the opened file and does not have several dimensions, so fid(:,2) will give a matrix dimensions exceeded error. You want to plot the data that came out of the file, KK in your case. Try this:
plot(t,KK(:,1),'b',t,KK(:,2),'r')
I also suspect you might have your indexing the wrong way round, although as your code is not minimal, complete and verifiable, it is difficult to say. You might find you need the following command:
plot(t,KK(1,:),'b',t,KK(2,:),'r')

Simplest way to read space delimited text file matlab

Ok, so I'm struggling with the most mundane of things I have a space delimited text file with a header in the first row and a row per observation and I'd like to open that file in matlab. If I do this in R I have no problem at all, it'll create the most basic matrix and voila!
But MATLAB seems to be annoying with this...
Example of the text file:
"picFile" "subjCode" "gender"
"train_1" 504 "m"
etc.
Can I get something like a matrix at all? I would then like to have MATLAB pull out some data by doing data(1,2) for example.
What would be the simplest way to do this?
It seems like having to write a loop using f-type functions is just a waste of time...
If you have a sufficiently new version of Matlab (R2013b+, I believe), you can use readtable, which is very much like how R does it:
T = readtable('data.txt','Delimiter',' ')
There are many functions for manipulating tables and converting back and forth between them and other data types such as cell arrays.
There are some other options in the data import and export section of the Statistics toolbox that should work in older versions of Matlab:
tblread: output in terms of separate variables for strings and numbers
caseread: output in terms of a char array
tdfread: output in terms of a struct
Alternatively, textscan should be able to accomplish what you need and probably will be the fastest:
fid = fopen('data.txt');
header = textscan(fid,'%s',3); % Optionally save header names
C = textscan(fid,'%s%d%s','HeaderLines',1); % Read data skipping header
fclose(fid); % Don't forget to close file
C{:}
Found a way to solve my problem.
Because I don't have the latest version of MATLAB and cannot use readable which would be the preferred option I ended up doing using textread and specifying the format of each column.
Tedious but maybe the "simplest" way I could find:
[picFile subCode gender]=textread('data.txt', '%s %f %s', 'headerlines',1);
T=[picFile(:) subCode(:) gender(:)]
The textscan solution by #horchler seems pretty similar. Thanks!

Reading complicated format CSV fileinto Matlab

My raw CSV file looks like the 1st pic. And I wants to use Matlab read it into the format as the 2rd pic. I have over 1000 the same kind of CSV files, it will be painful if I do it by copy/paste. How can I do this? Any examples?
raw data:
output data:
First thing to realize is that a .csv file has a very simple format. Your above file is actually a plain text file with the following text on each line:
id,A001
height
a1,a2,a3
3,4,5
3,4,5
6,7,5
weight
a1,a2,a3
4,4,5
5,4,6
i6,7,5
So it is not all that hard for you to write your own parser in Matlab. You want to use commands like
fid = fopen('filename.csv','r');
L = fgetl(fid); % get a text line from the file
commas = find(L==','); % find where the commas are in the line
n1 = str2num(L(1:commas(1)-1); % convert the first comma-delimited number on line L
fidout - fopen('myfile.csv','w');
Lout = [ L(commas(2)+1:commas(3)-1) ', a1, a1'];
fwrite(fidout,Lout); % write a line out to the output file
fclose all; % close all open files.
It will seem slow at first reading the various values in to various variables, and then arranging them to write out the way you want them written out to your output file. But once you get rolling it will go pretty fast and you will find yourself with a pretty good understanding of what is in files, and you will know first hand what is involved in writing something like texscan.m or csvwrite.m and so on.
Good luck!

Converting a series of .mat files into one CSV File

I hope this makes sense:
I have a series of .mat files that i want to use to train a weka classifier and I need to get these .mat files all into one big .CSV file to do so.
My files each contain an eigenvector which hast around 120 values.
Being brand spanking new to this enviroment, i wrote a simple script to concat all these vectos into one long vector, which doesnt really help me.
function c = csv()
for i = 1:99
if i>9
fname = strcat('Betas00', int2str(i));
else
fname = strcat('Betas000', int2str(i));
end
fext = strcat(fname, '.mat');
//fext
filename = sprintf("%s", fext);
load(filename);
dlmwrite('test.txt', fext, '-append');
dlmwrite('test.txt', ignmds, '-append');
end
Its not pretty, but can someone explain how i can use these basic concepts to do what i want? to get a CSV file which has 99 eigenvectors in it? Please? Pretty Please?
Hope that all makes sense!
It looks like you've done it already, what is left to do?
By the way you may want to replace your if with:
fname = strcat('Betas', num2str(i, '%-04.4d'));