Problem with creating a new .txt file.
Mostrar comentarios más antiguos
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.
Respuestas (1)
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 comentarios
Tommy
el 24 de Abr. de 2020
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)];
Ivan Mich
el 24 de Abr. de 2020
Tommy
el 24 de Abr. de 2020
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?
Ivan Mich
el 24 de Abr. de 2020
Tommy
el 25 de Abr. de 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);
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!