export matlab file to txt file with delimiter - matlab

I have 2 column vectors named t and f which looks like this:
t=
1
2
3
4
5
and
f=
10
20
30
40
50
I would like to make a txt file which looks like this:
1,10
2,20
3,30
4,40
5,50
I tried the next codes but nothing worked so far:
vec_char=char(44*ones(1,length(t)))'; %vector of comma's
filename=fopen('functiondata.txt','w');
formatspec='%d %s %d\n';
data={t,vec_char,f};
[nrows,ncols]=size(data);
for row=1:nrows
fprintf(filename,formatspec,data{row,:});
end
fclose(filename);
if theres something better to do, it would be nice.

I highly suggest you use dlmwrite. This takes a matrix as well as the name of the file you want to write and writes it to file. The default delimiter that separates the numbers are commas, which is what you want.
Try this:
t = (1:5).'; f = (10:10:50).'; %// Your data
dlmwrite('functiondata.txt', [t f]);
Opening up functiondata.txt, we get this:
1,10
2,20
3,30
4,40
5,50
Note
If you are using Windows, the new line character will not be put in properly when dlmwrite is called. You need to specifically state this when calling dlmwrite:
dlmwrite('functiondata.txt', [t f], 'newline', 'pc');

Related

MATLAB 'text' function not working with 'sprintf' argument

Trying to print the following range of labels in a figure:
aux = {'ca155.mat','ca154.mat','ca159.mat','ca146.mat','ca148.mat','ca004.mat'};
But I need it upper case and without the extension, so I use
text(0,0,upper(sprintf([aux{i},'\b\b\b\b'])));
In the command window I get the correct output such as for i=1, i.e. CA155. However the text function on a figure doesn't work and produces:
CA155.MAT[][][][]
Except instead of brackets there are closed rectangles (I couldn't copy the character).
How can I fix this?
When processing your text, you did not delete the extension, you inserted backspaces. Here some insights for demonstration:
>> x=upper(sprintf([aux{i},'\b\b\b\b']))
x =
'CA155'
>> size(x)
ans =
1 13
>> x(1:9)
ans =
'CA155.MAT'
>> x(1:10)
ans =
'CA155.MA'
The first 9 characters are still there but the following backspaces delete them when working in a command window. Looks like text does not support it, and backspaces are definitely not the way to go.
Use fileparts instead:
>> [filepath,name,ext]=fileparts(aux{i})
filepath =
0×0 empty char array
name =
'ca155'
ext =
'.mat'

How to load a csv file as a datamatrix in matlab?

I try to load a csv file in matlab to use a certain column as a vector for a OLS estimation. However, my csv looks like:
Date KCFSI
13 2004-02-01 -0.67
14 2004-03-01 -0.58
15 2004-04-01 -0.57
16 2004-05-01 -0.49
17 2004-06-01 -0.67
...
and I want to have the the column KCFSI as a vector.
I tried:
x=fopen('kcfsi.csv');
kcfsi=x(:,2);
But I don't even get a matrix for my x. Just get as value : "14" for whatever reason. I want to have something like "2x100"
csvread cannot open csv files containing non-Numeric values as stated in the documentation.
The file must contain only numeric values.
So you should use textscan as explained in this answer : https://stackoverflow.com/a/19613301/11756186
Alternatively you can use the readtable built-in function
csvtable = readtable('kcfsi.csv');
kcfsi_array = csvtable.KCFSI; %Column vector with the content of the KCFSI column
fopen returns a fileID, not a matrix.
Use A = readmatrix(filename) or M = csvread(filename) instead.

Read first line of CSV file that contains text in cells

I have a deco.csv file and I only want to extract B1 to K1 (20 columns of the first rows), i.e. Deco_0001 to Deco_0020.
I first make a pre-allocation:
names = string(20,1);
and what I want is when calling S(1), it gives Deco_0001; when calling S(20), it gives Deco_0020.
I have read through textscan but I do not know how to specify the range is first row and running from column 2 to column 21 of the csv file.
Also, I want save the names individually but what I have tried just save the first line in only one cell:
fid=fopen('deco.csv');
C=textscan(fid, '%s',1);
fclose(fid);
Thanks!
It's not very elegant, but this should work for you:
fid=fopen('deco.csv');
C=textscan(fid, '%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s',1,'Delimiter',',');
fclose(fid);
S = cell(20,1);
for ii = 1:20
S{ii} = C{ii+1};
end

Loading variables from ascii file

I am trying to load variables from a .dat file I have created.
The file is in the following format:
x = 1
y = 2
z = 3
I understand that if the file was in the format:
1 2 3
I could use
s = load(filename.dat)
and it would create an array with name 'S' storing all the numbers in the file.
However, from the first format I showed, I would like each stored as a separate variable.
I know I could do this with a .MAT file but this isn't really optimal to my requirements because it needs to be easily edited, preferably with notepad or another word processor.
Try textread function:
[varNames, varValues] = textread('tmp.txt', '%s%f', 'whitespace','\n', 'delimiter','=');
disp(varNames);
'x '
'y '
'z '
disp(varValues);
1
2
3

Converting a .txt file with 1 million digits of "e" into a vector in matlab

I have a text file with 1 million decimal digits of "e" number with 80 digits on each line excluding the first and the last line which have 76 and 4 digits and the file has 12501 lines. I want to convert it into a vector in matlab with each digit on each row. I tried num2str function, but the problem is that it gets converted like for example '7.1828e79' (13 characters). What can I do?
P.S.1: The first two lines of the text file (76 and 80 digits) are:
7182818284590452353602874713526624977572470936999595749669676277240766303535 47594571382178525166427427466391932003059921817413596629043572900334295260595630
P.S.2: I used "dlmread" and got a 12501x1 vector, with the first and second row of 7.18281828459045e+75 and 4.75945713821785e+79 and the problem is that when I use num2str for example for the first row value, I get: '7.182818284590453e+75' as a string and not the whole 76 digits. My aim was to do something like this:
e1=dlmread('e.txt');
es1=num2str(e1);
for i=1:12501
for j=1:length(es1(1,:))
a1((i-1)*length(es1(1,:))+j)=es1(i,j);
end
end
e_digits=a1.';
but I get a string like this:
a1='7.182818284590453e+754.759457138217852e+797.381323286279435e+799.244761460668082e+796.133138458300076e+791.416928368190255e+79 5...'
with 262521 characters instead of 1 million digits.
P.S.3: I think the problem might be solved if I can manipulate the text file in a way that I have one digit on each line and simply use dlmread.
Well, this is not hard, there are many ways to do it.
So first you want to load in your file as a Char Array using something simple like (you want a Char Array so that you can easily manipulate it to forget about the lines breaks) :
C = fileread('yourfile.txt'); %loads file as Char Array
D = C(~isspace(C)); %Removes SPACES which are line-breaks
Next, you want to actually append a SPACE between each char (this is because you want to use the num2str transform - and matlab needs to see the space), you can do this using a RESHAPE, a STRTRIM or simply a REGEX:
E = strtrim(regexprep(D, '.{1}', '$0 ')); %Add a SPACE after each Numeric Digit
Now you can transform it using str2num into a Vector:
str2num(E)'; %Converts the Char Array back to Vector
which will give you a single digit each row.
Using your example, I get a vector of 156 x 1, with 1 digit each row as you required.
You can get a digit per row like this
fid = fopen('e.txt','r');
c = textscan(fid,'%s');
c=cat(1,c{:});
c = cellfun(#(x) str2num(reshape(x,[],1)),c,'un',0);
c=cat(1,c{:});
And it is not the only possible way.
Could you please tell what is the final task, how do you plan using the array of e digits?