Save a string vector as csv in matlab - arrays

I have the following string array in matlab built the following way:
labels=textread(nome_tecnicas_base, '%s');
for i=1:size(labels)
temp_vector=cell(1,10);
[temp_vector{1:10}]=deal(labels{i});
final_vector=horzcat(final_vector,temp_vector);
end
I want to save this vector with each string value separated with commas (e.g., csv files) in a text file. I tried in several ways, but when I try to read it with, for example, the textread function i have the following error:
a=textread('labels-cpen-R.txt')
Error using dataread
Trouble reading number from file (row 1, field 1) ==> dct,dct,dct,dct,dct,dct,dct,dct,dct,dct,hierar
this is how my file was saved
dct,dct,dct,dct,dct,dct,dct,dct,dct,dct,hierarch-sift,hierarch-sift,hierarch-sift,hierarch-sift,hierarch-sift,hierarch-sift,hierarch-sift,hierarch sift,hierarch-sift,hierarch
sift,zernike,zernike,zernike,zernike,zernike,zernike,zernike,zernike,zernike,zernike,zernike2,zernike2,zernike2,zernike2,zernike2,zernike2,zernike2,zernike2,zernike2,zernike2,kpca,kpca,kpca,kpca,kpca,kpca,kpca,kpca,kpca,kpca,sift,sift,sift,sift,sift,sift,sift,sift,sift,sift,surf,surf,surf,surf,surf,surf,surf,surf,surf,surf,bayesianfusion0,bayesianfusion0,bayesianfusion0,bayesianfusion0,bayesianfusion0,bayesianfusion0,bayesianfusion0,bayesianfusion0,bayesianfusion0,bayesianfusion0,bks-fusion,bks-fusion,bks-fusion,bks-fusion,bks-fusion,bks-fusion,bks-fusion,bks-fusion,bks-fusion,bks-fusion,fusionvoting4,fusionvoting4,fusionvoting4,fusionvoting4,fusionvoting4,fusionvoting4,fusionvoting4,fusionvoting4,fusionvoting4,fusionvoting4,fusionvoting6,fusionvoting6,fusionvoting6,fusionvoting6,fusionvoting6,fusionvoting6,fusionvoting6,fusionvoting6,fusionvoting6,fusionvoting6,multiscale_voting,multiscale_voting,multiscale_voting,multiscale_voting,multiscale_voting,multiscale_voting,multiscale_voting,multiscale_voting,multiscale_voting,multiscale_voting,bks_rf_lvt,bks_rf_lvt,bks_rf_lvt,bks_rf_lvt,bks_rf_lvt,bks_rf_lvt,bks_rf_lvt,bks_rf_lvt,bks_rf_lvt,bks_rf_lvt,bks_svr_lvt,bks_svr_lvt,bks_svr_lvt,bks_svr_lvt,bks_svr_lvt,bks_svr_lvt,bks_svr_lvt,bks_svr_lvt,bks_svr_lvt,bks_svr_lvt,bks_svr_otsu,bks_svr_otsu,bks_svr_otsu,bks_svr_otsu,bks_svr_otsu,bks_svr_otsu,bks_svr_otsu,bks_svr_otsu,bks_svr_otsu,bks_svr_otsu,multiscale_bks_rf_lvt,multiscale_bks_rf_lvt,multiscale_bks_rf_lvt,multiscale_bks_rf_lvt,multiscale_bks_rf_lvt,multiscale_bks_rf_lvt,multiscale_bks_rf_lvt,multiscale_bks_rf_lvt,multiscale_bks_rf_lvt,multiscale_bks_rf_lvt,multiscale_bks_svr_lvt,multiscale_bks_svr_lvt,multiscale_bks_svr_lvt,multiscale_bks_svr_lvt,multiscale_bks_svr_lvt,multiscale_bks_svr_lvt,multiscale_bks_svr_lvt,multiscale_bks_svr_lvt,multiscale_bks_svr_lvt,multiscale_bks_svr_lvt
How can I save this vector and how can I read this file properly?

try textscan for reading and fprintf for writing
from the matlab documentation:
fileID = fopen('data.csv');
C = textscan(fileID,'%f %f %f %f %u8 %f',...
'Delimiter',',','EmptyValue',-Inf);
so in your case:
textscan(fileID,'%s', 'Delimiter', ',')
edit: for writing data to a file, you can use fprintf with a file identifier:
fileID= fopen('data.csv', 'w') ;
fprintf(fileID, '%s,', data{1,1:end-1}) ;
fprintf(fileID, '%s\n', data{1,end}) ;
fclose(fileID)

Related

Coverting text file to array in IDL

I was looking for a way to convert the following textfile:
7.73E+01
7.29E+01
7.06E+00
6.84E+00
6.70E+00
6.45E+00
6.12E+00
5.93E+00
Into an IDL array
array = [7.73E+01,7.29E+01,7.06E+00,6.84E+00,6.70E+00,6.45E+00,6.12E+00,5.93E+00]
Thanks in advance for the help.
For a simple file like you show, you can just do:
n_lines = file_lines(filename)
array = fltarr(n_lines)
open, lun, filename, /get_lun
readf, lun, array
free_lun, lun
It can get more complicated if the text file is not just a single value per line and every line is part of the data.

Adding numbers to each line in a file in haxe language

Need some help, writing a program that will read the text from a file, but will
put at the beginning of each line a number, in such a way that each line is numbered in ascending order
example:
file1
a
b
c
What I want to see:
1: a
2: b
3: c
Process:
Read contents of file into a String
Split by line ending into Array<String>
Iterate and mutate contents line by line
Join by line ending back into a String
Write back into file
Sample code for any sys target:
var arr = sys.File.getContent('file.txt').split("\n");
for(i in 0...arr.length) {
arr[i] = (i+1) + ": " + arr[i];
}
sys.File.saveContent('file.txt', arr.join("\n"));

Matlab: copy array in file txt

I want to copy elements of an array in a file txt. I read the first 50 samples of a wav file in array a and I want to copy a in a file txt. This is my code:
[s,fs]=wavread('file.wav');
for k=1:50
a=s(k)
end
fid = fopen('file.txt','wt');
fprintf(fid,'%f\n',a);
fclose(fid);
With this code in file txt there is only the last element of a, not all samples.
fprintf can process vector input. So you can simply replace the loop by a = s(1:50);:
[s,fs]=wavread('file.wav');
a = s(1:50);
fid = fopen('file.txt','wt');
fprintf(fid,'%f\n',a);
fclose(fid);

Reading JLD files

I have encountered this problem when reading a JLD file. I have successfully created the file as follows:
using JLD, HDF5
for i in 1:10
file = jldopen("/MY PATH/mydata.jld", "w")
write(file, "A", vector[i] for i in 10 )
close(file)
end
but when I read the file using the following instructions:
file = jldopen("/My PATH/my_tree/mydata.jld", "r")
For this first instruction,it's executed correctly, but when I execute the following:
read(file, "A")
I got this error:
WARNING: type Base.Generator{Core.Int64,##1#2} not present in workspace; reconstructing
ERROR: MethodError: no method matching julia_type(::Void)
in _julia_type(::ASCIIString) at /root/.julia/v0.5/JLD/src/JLD.jl:966
in julia_type(::ASCIIString) at /root/.julia/v0.5/JLD/src/JLD.jl:32
in jldatatype(::JLD.JldFile, ::HDF5.HDF5Datatype) at /root/.julia/v0.5/JLD/src/jld_types.jl:672
in reconstruct_type(::JLD.JldFile, ::HDF5.HDF5Datatype, ::ASCIIString) at /root/.julia/v0.5/JLD/src/jld_types.jl:737
in jldatatype(::JLD.JldFile, ::HDF5.HDF5Datatype) at /root/.julia/v0.5/JLD/src/jld_types.jl:675
in read(::JLD.JldDataset) at /root/.julia/v0.5/JLD/src/JLD.jl:381
in read(::JLD.JldFile, ::ASCIIString) at /root/.julia/v0.5/JLD/src/JLD.jl:357
in eval(::Module, ::Any) at ./boot.jl:237
vector[i] for i in 10 creates a generator, which JLD happily writes to the file for you. You probably want an array, so wrap that expression in collect.

Read text file as numpy array using np.loadtxt

I am trying to read a text file as a numpy array. For some reason one of the files is read fine, but an error (X = np.array(X, dtype) ValueError: setting an array element with a sequence.) is reported for another.
The code is:
freq_chan = np.loadtxt(os.path.join(dirs,fil), skiprows = 6+int(no_nodes))
The row of the file that is read is:
45.000000000000 1.73145123922036E-002 -2.27352994577858E-004 0.0000000000000 0.0000000000000 0.0000000000000 0.0000000000000
and the row of the file that is not read is:
450.00000000000 1.75123936984107E-003 4.99078580749004E-004 -1.01870220257046E-005 -1.25748632064143E-005 4.53694668200015E-004 1.75279359420616E-003 1.06388230080026E-005 1.25165432922695E-005 -1.26393875391086E-003
What might be the reason for this?
Thanks
I suspect that there is a problem with your delimiter character at least in the first file. try to set the delimiter argument.
Take a look to this explanation

Resources