Error using "fopen"

I'm trying to save the current plot as .jpg. The code is the following :
filePath = 'C:\Users\leo-9\Desktop\roba pc nuovo\dottorato\Food network\plots\';
addpath(filePath);
name = strcat(filePath,plot_name,'.jpg');%num2str(itt)
%name = strcat('Figures_v2023\Stats\','Fig_Stats_',items_veg_name(stepp) );
saveas(gcf,name);
The error reported is the following :
Error using fopen
Unable to find file. Ensure file exists and path is valid.
Error in matlab.graphics.internal.name (line 94)
fidread = fopen(pj.FileName,'r');
Error in print (line 71)
pj = matlab.graphics.internal.name( pj );
Error in saveas (line 181)
print( h, name, ['-d' dev{i}] )
Error in BPR_plot_stats (line 41)
saveas(gcf,name);
Can you help me please?

4 件のコメント

Dyuman Joshi
Dyuman Joshi 2024 年 1 月 28 日
Most likely cause of the error is a missing delimiter in the file name generated via concantenation.
Use fullfile instead of strcat().
If the error still does not resolve, then please share the full code.
Yash
Yash 2024 年 2 月 5 日
Hi Leonardo,
The error you're encountering is likely related to an incorrect file path or permission restrictions. The MATLAB function "fopen" (which is called internally) is unable to read at the particular file location.
I tried to execute a similar code, ensuring that the file path was accurate, and did not experience any errors. Kindly share your full code so that we can reproduce the issue and assist you better.
Stephen23
Stephen23 2024 年 2 月 5 日
You should use FULLFILE, just as Dyuman Joshi correctly recommended.
Also: avoid polluting the MATLAB seach path with folders of data files. In other words, get rid of ADDPATH.
Walter Roberson
Walter Roberson 2024 年 2 月 5 日
Possibly plot_name is not valid -- for example it might be numeric.

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

回答 (1 件)

Jaimin
Jaimin 2024 年 8 月 13 日

0 投票

Based on the code and the error message, it appears that the provided path may be incorrect, or you may not have the necessary permissions to write to the desired folder.
Another approach you might consider is using the fullfile function in MATLAB. This function helps generate the path string based on the platform (e.g., Windows or UNIX), ensuring compatibility.
Here, I have provided the updated code using the “fullfile” function:
filePath = <BASE PATH>;
name = fullfile(filePath,'xyz.jpg');
saveas(gcf,name);
You can find more information on using the “fullfile” function in the following documentation:

1 件のコメント

Melissa
Melissa 2024 年 12 月 18 日
@basket random, Thank you very much!

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

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

製品

リリース

R2023b

タグ

質問済み:

2024 年 1 月 28 日

コメント済み:

2024 年 12 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by