Problem with creating a new .txt file.

5 vues (au cours des 30 derniers jours)
Ivan Mich
Ivan Mich le 24 Avr 2020
Commenté : Tommy le 25 Avr 2020
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.

Réponses (1)

Tommy
Tommy le 24 Avr 2020
Modifié(e) : Tommy le 24 Avr 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 commentaires
Ivan Mich
Ivan Mich le 24 Avr 2020
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
Tommy
Tommy le 25 Avr 2020
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);

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by