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.
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)
I want to read data from a file and save it into an array. Then insert some new data into this array and then save this new data back into the same file deleting what is already there. My code works perfectly, giving me my required data, when I have 'r+' in the fopen parameters, however when I write to the file again it does not delete the data already in the file just appends it to the end as expected. However when I change the permissions to 'w+' instead of 'r+', my code runs but no data is read in or wrote to the file! Anyone know why this might be the case? My code is as seen below.
N = 1021;
b = [0;0;0;0;0];
% Opens file specified by user.
fid = fopen('testing','w+');
% Read header data
Header = fread(fid, 140);
% Move to start of data
fseek(fid,140,'bof');
% Read from end of config header to end of file and save it in an array
% called data
Data = fread(fid,inf);
Data=reshape(Data,N,[]);
b=repmat(b,[1 size(Data,2)]);
r=[b ; Data];
r=r(:);
r = [Header;r];
% write new values into file
fwrite(fid,r);
fclose(fid);
% Opens file specified by user.
fid = fopen('test');
All = fread(fid,inf);
fclose(fid);
According to the documentation, the w+ option allows you to "Open or create new file for reading and writing. Discard existing contents, if any." The contents of the file are discarded, so Data and Header are empty.
You need to set the position indicator of the filehandle before writing. With frewind(fid) you can set it to the beginning of the file, otherwise the file is written / appended at the current position.
N = 1021;
b = [0;0;0;0;0];
% Opens file specified by user.
fid = fopen('testing','r+');
% Read header data
Header = fread(fid, 140);
% Move to start of data
fseek(fid,140,'bof');
% Read from end of config header to end of file and save it in an array
% called data
Data = fread(fid,inf);
Data=reshape(Data,N,[]);
b=repmat(b,[1 size(Data,2)]);
r=[b ; Data];
r=r(:);
r = [Header;r];
% write new values into file
frewind(fid);
fwrite(fid,r);
fclose(fid);
% Opens file specified by user.
fid = fopen('test');
All = fread(fid,inf);
fclose(fid);
I have 20 text files, and I want to use a matlab loop to get the last line of each file without taking into consideration the other lines. is there any matlab command to solve this problem?
One thing you can try is to open the text file as a binary file, seek to the end of the file, and read single characters (i.e. bytes) backwards from the end of the file. This code will read characters from the end of the file until it hits a newline character (ignoring a newline if it finds it at the very end of the file):
fid = fopen('data.txt','r'); %# Open the file as a binary
lastLine = ''; %# Initialize to empty
offset = 1; %# Offset from the end of file
fseek(fid,-offset,'eof'); %# Seek to the file end, minus the offset
newChar = fread(fid,1,'*char'); %# Read one character
while (~strcmp(newChar,char(10))) || (offset == 1)
lastLine = [newChar lastLine]; %# Add the character to a string
offset = offset+1;
fseek(fid,-offset,'eof'); %# Seek to the file end, minus the offset
newChar = fread(fid,1,'*char'); %# Read one character
end
fclose(fid); %# Close the file
On Unix, simply use:
[status result] = system('tail -n 1 file.txt');
if isstrprop(result(end), 'cntrl'), result(end) = []; end
On Windows, you can get the tail executable from the GnuWin32 or UnxUtils projects.
It may not be very efficient, but for short files it can be sufficient.
function pline = getLastTextLine(filepath)
fid = fopen(filepath);
while 1
line = fgetl(fid);
if ~ischar(line)
break;
end
pline = line;
end
fclose(fid);