Hi,
I am getting warning with fprintf function in my script as specify below, I reallly don't how to figure out. I am a new matlab user
Thanks for your help
fprintf(fileID, " pointList.append(eval('(%s)' %line.strip())))\n");
error / warning : The format might not agree with the argument count

 採用された回答

Star Strider
Star Strider 2022 年 8 月 4 日

1 投票

It looks as though you want to print a percent (%) sign.
Try this —
fileID = 1; % Print To Command Window
fprintf(fileID, " pointList.append(eval('(%s)' %%line.strip())))\n", 'Hi, There!');
pointList.append(eval('(Hi There!)' %line.strip())))
In the fprintf documentation see the formatSpec section and specifically Text Before or After Formatting Operators (no direct link to it) to understand how to do this correctly.
I have no idea what the string is supposed to be, so I created one to test the code!
.

5 件のコメント

Rigo ZOO
Rigo ZOO 2022 年 8 月 4 日
Thanks Star,
In fact this my script in python:
for line in lines:
pointList.append(eval('(%s)' %line.strip()))
I just want fprintf matlab function to print this python script. With your suggestion below the script printed and is not exact
pointList.append(eval('()'
Cheers
Rigo ZOO
Rigo ZOO 2022 年 8 月 4 日
Hi Star,
When I use your suggestion as below
fprintf(fileID, " pointList.append(eval('(%s)' %%line.strip())))\n", '')
Below the printed script without this %s. How can I manage to get %s in the printed script?
pointList.append(eval('()' %%line.strip())))
Thanks
Star Strider
Star Strider 2022 年 8 月 4 日
If you want both percent signs printed, do exactly as I did earlier —
fileID = 1; % Print To Command Window
fprintf(fileID, " pointList.append(eval('(%%s)' %%line.strip())))\n")
pointList.append(eval('(%s)' %line.strip())))
A single percent sign is interpreted as a control character.
.
Rigo ZOO
Rigo ZOO 2022 年 8 月 4 日
Star,
Great, thanks again it works.
Cheers
Star Strider
Star Strider 2022 年 8 月 4 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2022 年 8 月 4 日
編集済み: Fangjun Jiang 2022 年 8 月 4 日

0 投票

use single quote mark, not double quote mark
a=1.2;
fid=1
fid = 1
fprintf(fid, 'this is my number %f', a)
this is my number 1.200000

1 件のコメント

Rigo ZOO
Rigo ZOO 2022 年 8 月 4 日
Thanks Fangjun,
But this option doesn't work either, I have already a simple quote '(%s)' . This option simple generte Invlaid syntax error.
Cheers

サインインしてコメントする。

Image Analyst
Image Analyst 2022 年 8 月 4 日

0 投票

The problem is you're using %s but not passing in any string after the format string. It needs to be of this form
fprintf(fid, 'stuff blah blah %s more stuff.\n', yourString);
The fprintf is seeing %s but not seeing yourString. Essentially you have
fprintf(fileID, " stuff %s more stuff\n");
as far as the mlint syntax checker is concerned. What is really wants to see is
fprintf(fileID, " stuff %s more stuff\n", yourString);
What you should do is this:
% Evaluate the expression with the hated eval
someNumber = eval(someString);
% Say someNumber is 5 and you'll use that as an index to the pointList.append array
yourString = sprintf(' pointList.append(eval(%d) %%line.strip())))\n', someNumber)
% Now write that string to the file.
fprintf(fileID, "%s\n", yourString);

1 件のコメント

Rigo ZOO
Rigo ZOO 2022 年 8 月 4 日
Thanks Image Analyst,
This method works as well
Cheers

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2019a

質問済み:

2022 年 8 月 4 日

コメント済み:

2022 年 8 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by