whay zero is not consider by %d identifier - matlab

I Have 400 files in my directory. Which have file name H1001,H1002,H1003....like that. I want to read that files in matlab
When I m using that code it give me the error.
'd=dir('C:\Users\Desktop\New\*.txt')>
<num_files=length(d)>
data=cell(1,num_files);
for k = 1:400
myfilename = sprintf('H1%3d.txt',k);
mydata{k} = importdata(myfilename);
end'
it is showing
myfilename=H1 1. which is wrong file name H1001. so GETTING ERROR IN NEXT LINE.
It is not reading 00. It gives blank space.
Can any body tell me the answer.

The correct format string if you want a zero filled right adjusted value is not %3d (which right adjusts with spaces) but instead %03d;
for k = 1:400
myfilename = sprintf('H1%03d.txt',k);
mydata{k} = importdata(myfilename);
end
The difference can easily be seen in
> printf('H1%3d.txt\n', 7);
H1 7.txt
> printf('H1%03d.txt\n', 7);
H1007.txt

Related

Problem adding tow digits to path string to read multiple files

I want to read multiple files from a folder, the only problem in my code down is that
instead of having the full image path:
'checkboard/Gaussian_Noised/image_01.jpg'
I have the path without the zero in the last two digits
EDIT: There is space instead of zero on the first 9 Image paths.
'checkboard/Gaussian_Noised/image_ 1.jpg'
how can I fix this please
%-------------------------------------------------------
clc
clear all
close all
%------------------------------------------------------
path = 'checkboard/Gaussian_Noised/';
% Define images to process
imageFileNames = cell(1,36);
for n = 1:36
imageFileNames{n} = strcat(path,sprintf('image_%2d.jpg',n))
end
%------------------------------------------------------
In this case, it is better to use the float-format-specifier to pad zeros:
num2str(1,'%02.f')
'01'
num2str(1,'%02.d')
'1'
num2str(1,'%2.d')
'1'
num2str(1,'%d')
'1'
So in your case:
sprintf('image_%02.f.jpg',n)
general advice
As a general advice, you may want just to check the folder for all files of a certain pattern
path2dir = pwd; % path to working directory
pattern = 'image_*.jpg'
Lst = dir(fullfile(path2dir ,pattern));
for i = 1:length(Lst)
path2file = fullfile(Lst(i).folder,Lst(i).name);
% load the file and do something with it
end

Read textfile with a mix of floats, integers and strings in the same column

Loading a well formatted and delimited text file in Matlab is relatively simple, but I struggle with a text file that I have to read in. Sadly I can not change the structure of the source file, so I have to deal with what I have.
The basic file structure is:
123 180 (two integers, white space delimited)
1.5674e-8
.
.
(floating point numbers in column 1, column 2 empty)
.
.
100 4501 (another two integers)
5.3456e-4 (followed by even more floating point numbers)
.
.
.
.
45 String (A integer in column 1, string in column 2)
.
.
.
A simple
[data1,data2]=textread('filename.txt','%f %s', ...
'emptyvalue', NaN)
Does not work.
How can I properly filter the input data? All examples I found online and in the Matlab help so far deal with well structured data, so I am a bit lost at where to start.
As I have to read a whole bunch of those files >100 I rather not iterate trough every single line in every file. I hope there is a much faster approach.
EDIT:
I made a sample file available here: test.txt (google drive)
I've looked at the text file you supplied and tried to draw a few general conclusions -
When there are two integers on a line, the second integer corresponds to the number of rows following.
You always have (two integers (A, B) followed by "B" floats), repeated twice.
After that you have some free-form text (or at least, I couldn't deduce anything useful about the format after that).
This is a messy format so I doubt there are going to be any nice solutions. Some useful general principles are:
Use fgetl when you need to read a single line (it reads up to the next newline character)
Use textscan when it's possible to read multiple lines at once - it is much faster than reading a line at a time. It has many options for how to parse, which it is worth getting to know (I recommend typing doc textscan and reading the entire thing).
If in doubt, just read the lines in as strings and then analyse them in MATLAB.
With that in mine, here is a simple parser for your files. It will probably need some modifications as you are able to infer more about the structure of the files, but it is reasonably fast on the ~700 line test file you gave.
I've just given the variables dummy names like "a", "b", "floats" etc. You should change them to something more specific to your needs.
function output = readTestFile(filename)
fid = fopen(filename, 'r');
% Read the first line
line = '';
while isempty(line)
line = fgetl(fid);
end
nums = textscan(line, '%d %d', 'CollectOutput', 1);
a = nums{1}(1);
b = nums{1}(2);
% Read 'b' of the next lines:
contents = textscan(fid, '%f', b);
floats1 = contents{1};
% Read the next line:
line = '';
while isempty(line)
line = fgetl(fid);
end
nums = textscan(line, '%d %d', 'CollectOutput', 1);
c = nums{1}(1);
d = nums{1}(2);
% Read 'd' of the next lines:
contents = textscan(fid, '%f', d);
floats2 = contents{1};
% Read the rest:
rest = textscan(fid, '%s', 'Delimiter', '\n');
output.a = a;
output.b = b;
output.c = c;
output.d = d;
output.floats1 = floats1;
output.floats2 = floats2;
output.rest = rest{1};
end
You can read in the file line by line using the lower-level functions, then parse each line manually.
You open the file handle like in C
fid = fopen(filename);
Then you can read a line using fgetl
line = fgetl(fid);
String tokenize it on spaces is probably the best first pass, storing each piece in a cell array (because a matrix doesn't support ragged arrays)
colnum = 1;
while ~isempty(rem)
[token, rem] = strtok(rem, ' ');
entries{linenum, colnum} = token;
colnum = colnum + 1;
end
then you can wrap all of that inside another while loop to iterate over the lines
linenum = 1;
while ~feof(fid)
% getl, strtok, index bookkeeping as above
end
It's up to you whether it's best to parse the file as you read it or read it into a cell array first and then go over it afterwards.
Your cell entries are all going to be strings (char arrays), so you will need to use str2num to convert them to numbers. It does a good job of working out the format so that might be all you need.

Modify the value of a specific position in a text file in Matlab

AIR, ID
AIR.SIT
50 1 1 1 0 0 2 1
43.57 -116.24 1. 857.7
Hi, All,
I have a text file like above. Now in Matlab, I want to create 5000 text files, changing the number "2" (the specific number in the 3rd row) from 1 to 5000 in each file, while keeping other contents the same. In every loop, the changed number is the same with the loop number. And the output in every loop is saved into a new text file, with the name like AIR_LoopNumber.SIT.
I've spent some time writing on that. But it is kind of difficult for a newby. Here is what I have:
% - Read source file.
fid = fopen ('Air.SIT');
n = 1;
textline={};
while (~feof(fid))
textline(n,1)={fgetl(fid)};
end
FileName=Air;
% - Replace characters when relevant.
for i = 1 : 5000
filename = sprintf('%s_%d.SIT','filename',i);
end
Anybody can help on finishing the program?
Thanks,
James
If your file is so short you do not have to read it line by line. Just read the full thing in one variable, modify only the necessary part of it before each write, then write the full variable back in one go.
%% // read the full file as a long sequence of 'char'
fid = fopen ('Air.SIT');
fulltext = fread(fid,Inf,'char') ;
fclose(fid) ;
%% // add a few blank placeholder (3 exactly) to hold the 4 digits when we'll be counting 5000
fulltext = [fulltext(1:49) ; 32 ; 32 ; 32 ; fulltext(50:end) ] ;
idx2replace = 50:53 ; %// save the index of the characters which will be modified each loop
%% // Go for it
baseFileName = 'AIR_%d.SIT' ;
for iFile = 1:1000:5000
%// build filename
filename = sprintf(baseFileName,iFile);
%// modify the string to write
fulltext(idx2replace) = num2str(iFile,'%04d').' ; %//'
%// write the file
fidw = fopen( filename , 'w' ) ;
fwrite(fidw,fulltext) ;
fclose(fidw) ;
end
This example works with the text in your example, you may have to adjust slightly the indices of the characters to replace if your real case is different.
Also I set a step of 1000 for the loop to let you try and see if it works without writing 1000's of file. When you are satisfied with the result, remove the 1000 step in the for loop.
Edit:
The format specifier %04d I gave in the first solution insure the output will take 4 characters, and it will pad any smaller number with zero (ex: 23 => 0023). It is sometimes desirable to keep the length constant, and in your particular example it made things easier because the output string would be exactly the same length for all the files.
However it is not mandatory at all, if you do not want the loop number to be padded with zero, you can use the simple format %d. This will only use the required number of digits.
The side effect is that the output string will be of different length for different loop number, so we cannot use one string for all the iterations, we have to recreate a string at each iteration. So the simple modifications are as follow. Keep the first paragraph of the solution above as is, and replace the last 2 paragraphs with the following:
%% // prepare the block of text before and after the character to change
textBefore = fulltext(1:49) ;
textAfter = fulltext(51:end) ;
%% // Go for it
baseFileName = 'AIR_%d.SIT' ;
for iFile = 1:500:5000
%// build filename
filename = sprintf(baseFileName,iFile);
%// rebuild the string to write
fulltext = [textBefore ; num2str(iFile,'%d').' ; textAfter ]; %//'
%// write the file
fidw = fopen( filename , 'w' ) ;
fwrite(fidw,fulltext) ;
fclose(fidw) ;
end
Note:
The constant length of character for a number may not be important in the file, but it can be very useful for your file names to be named AIR_0001 ... AIR_0023 ... AIR_849 ... AIR_4357 etc ... because in a list they will appear properly ordered in any explorer windows.
If you want your files named with constant length numbers, the just use:
baseFileName = 'AIR_%04d.SIT' ;
instead of the current line.

Matlab fopen, is it possible to have a numeric file name?

I have the following code:
ptol = [2, 4, 8, ...];
a = ptol(1)
fid = fopen( a,'r');
I need to open a file determined by which number is called from ptol, i.e. if ptol(1) = 2, then fopen should open file 2.
Currently I get the error "invalid filename". How do I fix this?
The following code is what I need to use to "load" the data in the files I'm struggling to open in to a matrix.
fileName = strcat(num2str(a),'.ext');
file = fopen(fileName,'r');
count = 1;
lines2skip = 4;
mat = zeros(29,872);
while ~feof(file)
if count <= lines2skip
count = count+1;
[~] = fgets(file); % throw away unwanted line
continue;
else
line = strtrim(fgets(file));
mat = [mat ;cell2mat(textscan(line, '%f')).'];
count = count +1;
end
end
a is a number, I guess.
Thus, you need to specify a string which corresponds to the file name.
Does the file have any extension? num2str and strcat should do the magic.
The code:
fileName = strcat(num2str(a),'.ext');
fid = fopen(fileName,'r');
Notice that .ext has to be replace with the actual extension. If you are using .txt files, then replace with .txt.
Also, check for the position of the file (you need to specify the exact path).

Skipping Lines in Matlab Code

I am trying to create a Matlab code that strips the header off of a text file and proceeds in the pattern of recording a line and skipping the next three lines.
I have figured out how to strip the header, but do not know how to code so that the program only records lines 1 (after the header is removed) 5,9, 13, etc.
Any recommendations?
I don't know how your data is formatted, so the actual code you use may differ, but this should give you the idea
file_lines = {};
fid = fopen(filename);
while 1
text_line = fgetl(fid);
%quit reading on an empty line
if ~ischar(text_line)
break
end
%keep the lines that 1 as the first value (this is what you wanted, right?)
data_line = str2num(text_line)
if(data_line(1,1) == 1)
file_lines{end+1} = data_line;
end
end
fclose(fid);