Problem with creating a new .txt file.

Hello
I have a problem with a code. Well, I would like to add to a specific line of a file one more number. The thrid line has this content:
"oficcial".
After using code I would like the third line to have this format:
"oficcial 25".
In order to make it I am using these commands,
sm=regexp(fileread('orders.txt'), '\r?\n', 'split') .';
replaced_lines=[sm(1:2);sm(3)"25"; sm(4:5)]
fid = fopen('new.txt', 'wt');
fprintf(fid, '%s\n', replaced_lines{:});
fclose(fid)
but command window shows me error:
Invalid use of operator.
Could anyone help me?
I am importing two files in order to understand the problem.

Answers (1)

Tommy
Tommy on 24 Apr 2020
Edited: Tommy on 24 Apr 2020
You'll need to get the character vector contained within the 1x1 cell array sm(3) by instead using sm{3}, then horizontally concatenate '25', then place the result back in a cell array so it can be concatenated with sm(1:2) and sm(4:5):
sm=regexp(fileread('orders.txt'), '\r?\n', 'split') .';
replaced_lines=[sm(1:2);{[sm{3} '25']}; sm(4:5)];
fid = fopen('new.txt', 'wt');
fprintf(fid, '%s\n', replaced_lines{:});
fclose(fid);
(edit) The following is simpler and would also work:
sm=regexp(fileread('orders.txt'), '\r?\n', 'split') .';
sm{3} = [sm{3} '25'];
fid = fopen('new.txt', 'wt');
fprintf(fid, '%s\n', sm{:});
fclose(fid);

6 Comments

Ok, one question.. If I want to use a loop in order to put a spesific value by each iteration How could I write it?
I mean I have a value, M=66. I am writing this command:
replaced_lines=[sm(1:2);{[sm{3} 'M']}; sm(4:5)];
in order to replace the line 3, with the line:
oficcial 66.
But Command window shows me the following error:
Undefined function or variable 'sm'
Could you help me?
Have you run the line
sm=regexp(fileread('orders.txt'), '\r?\n', 'split') .';
first in order to obtain sm?
Also, if M equals 66, to get your desire output you'll need
replaced_lines=[sm(1:2);{[sm{3} num2str(M)]}; sm(4:5)];
Unfortunately I have tried the commands you suggested , before I sent you in order not to annoy you again, but it not works..
Could you suggest me something else?
Can you post all of your code here? I'm not entirely sure what you meant by "I want to use a loop in order to put a spesific value by each iteration" Are you trying to create multiple new text files, one per iteration?
ok, I have one txt file ( I am importing it as "file1.txt"). I want from this file to take each value, putting it in the point I mentioned and creating a new txt file.
clc
clear
sm=regexp(fileread('orders.txt'), '\r?\n', 'split') .';
M=regexp(fileread('file1.txt'), '\r?\n', 'split') .';
for i=1:size(M,1)
replaced_lines=[sm(1:2);{[sm{3} num2str(M(i,:)]}; sm(4:5)];
fid = fopen('new%d.txt', i+1);
fprintf(fid, '%s\n', replaced_lines{:});
fclose(fid);
end
an example of one file that comes out from this code is the file new1.txt that I am importing too.
Thank you in advance
Okay I see. A few comments:
(1) As M is a cell array, just like sm, then M(i) will give you another cell array. To obtain the data within M, you should use M{i}.
(2) I did not realize that your numbers were coming from another file. Your code stores the numbers as character vectors within M, rather than as doubles like I initially assumed, so the call to num2str isn't needed.
(3) In this line:
fid = fopen('new%d.txt', i+1);
you are passing i+1, a double, as the second argument to fopen. If you check the docs, that is not a valid second argument to pass to fopen. It looks like you are trying to format the filename by placing i+1 where the '%d' is located. In that case, you should use sprintf. fopen alone doesn't know anything about formatting.
(4) Per the fopen documentation:
"If you open a file with write or append access and the file is not in the current folder, then fopen creates a file in the current directory."
Here is where we need the second argument to fopen. I believe any permission other than just 'r' (the default) counts as some form of writing or appending.
So, the following should work:
sm=regexp(fileread('orders.txt'), '\r?\n', 'split') .';
M=regexp(fileread('file1.txt'), '\r?\n', 'split') .';
for i=1:size(M,1)
replaced_lines=[sm(1:2);{[sm{3} M{i}]}; sm(4:5)];
fid = fopen(sprintf('new%d.txt', i+1), 'w');
fprintf(fid, '%s\n', replaced_lines{:});
fclose(fid);
end
fid = fopen('new%d.txt', i+1);

Sign in to comment.

Categories

Asked:

on 24 Apr 2020

Commented:

on 25 Apr 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!