figure saving in created folder. Error using savepath: invalid or missing path
8 views (last 30 days)
Show older comments
Andrea Giordano
on 6 Oct 2022
Moved: Walter Roberson
on 11 Oct 2022
While running a for loop, I create folders which name depend on the external looping variable (j). Folders creation works just fine, but if I try to save my gcf into the inner folder, I receive an error stating "Error using saveas (line 138)
Invalid or missing path: ../data_analysis/CD_plot/flow_speed_10/CD_plot_#1flow_speed10".
On the other hand, if I try to save my gcf into the current working folder "../data_analysis" or into the mid folder "../data_analysis/CD_plot", the code works fine. I do not understand what the issue is. Could it be that the just created inner folder does not yet pop up as a possible recipient directory? I highly doubt this.
Below, my for loop code.
[status, msg, msgID] = mkdir('CD_plot');
% sz = linspace(1,100,200);
for j = 1:length(sel_speed)
[status, msg, msgID] = mkdir(sprintf('../data_analysis/CD_plot/ flow_speed_%d', sel_speed(j)));
dyn_pressure = 0.5 * rho * sel_speed(j) ^ 2; % calculation of dynamic pressure
div = dyn_pressure * S;
clear k1 k2 k3 k4 k5
figure('Position', [200, 200, 800, 800])
title(['CD plot # ', num2str(j), '; Flow Speed: ', num2str(sel_speed(j))],'fontweight','bold','fontsize', 24)
hold on
grid on
xlabel('AoA [deg]','fontweight','bold','fontsize', 20);
ylabel('CD [ ]','fontweight','bold','fontsize', 20);
xlim([-10 35])
% ylim([-7 1])
for k = 1:length(MyFolderInfo)
if k == 87
continue
end
if (exp_value.vel(k) == sel_speed(j)) && (exp_value.inflation(k) == sel_inflation(1))
scatter(exp_value.aoa(k), exp_value.f_avg(k, 1) / div, 'or', 'filled', 'LineWidth',5)
if exist('k1','var') == 1
x_vec = [exp_value.aoa(k1), exp_value.aoa(k)];
y_vec = [exp_value.f_avg(k1, 1) / div, exp_value.f_avg(k, 1) / div];
plot(x_vec, y_vec, '--r')
end
k1 = k;
elseif (exp_value.vel(k) == sel_speed(j)) && (exp_value.inflation(k) == sel_inflation(2))
scatter(exp_value.aoa(k), exp_value.f_avg(k, 1) / div, 'ok', 'filled')
if exist('k2','var') == 1
x_vec = [exp_value.aoa(k2), exp_value.aoa(k)];
y_vec = [exp_value.f_avg(k2, 1) / div, exp_value.f_avg(k, 1) / div];
plot(x_vec, y_vec, '--k')
end
k2 = k;
elseif (exp_value.vel(k) == sel_speed(j)) && (exp_value.inflation(k) == sel_inflation(3))
scatter(exp_value.aoa(k), exp_value.f_avg(k, 1) / div, 'om', 'filled')
if exist('k3','var') == 1
x_vec = [exp_value.aoa(k3), exp_value.aoa(k)];
y_vec = [exp_value.f_avg(k3, 1) / div, exp_value.f_avg(k, 1) / div];
plot(x_vec, y_vec, '--m')
end
k3 = k;
elseif (exp_value.vel(k) == sel_speed(j)) && (exp_value.inflation(k) == sel_inflation(4))
scatter(exp_value.aoa(k), exp_value.f_avg(k, 1) / div, 'ob', 'filled')
if exist('k4','var') == 1
x_vec = [exp_value.aoa(k4), exp_value.aoa(k)];
y_vec = [exp_value.f_avg(k4, 1) / div, exp_value.f_avg(k, 1) / div];
plot(x_vec, y_vec, '--b')
end
k4 = k;
elseif (exp_value.vel(k) == sel_speed(j)) && (exp_value.inflation(k) == sel_inflation(5))
scatter(exp_value.aoa(k), exp_value.f_avg(k, 1) / div, 'og', 'filled')
if exist('k5','var') == 1
x_vec = [exp_value.aoa(k5), exp_value.aoa(k)];
y_vec = [exp_value.f_avg(k5, 1) / div, exp_value.f_avg(k, 1) / div];
plot(x_vec, y_vec, '--g')
end
k5 = k;
end
end
legend({'inf. = 0 mL', 'inf. = 60 mL', 'inf. = 90 mL', 'inf. = 120 mL', 'inf. = 30 mL'}, ...
'Location','north','Orientation','horizontal','fontsize', 16)
hold off
saveas(gcf, ['../data_analysis/CD_plot/flow_speed_', num2str(sel_speed(j)),'/CD_plot_#', num2str(j), 'flow_speed', num2str(sel_speed(j))], 'svg');
end
Accepted Answer
Fangjun Jiang
on 10 Oct 2022
Moved: Walter Roberson
on 11 Oct 2022
This is really dumb.
In your code, the mkdir() line, there is an extra whitespace in front of 'flow_speed'.
So you created a folder called ' flow_speed_10' (with the leading whitespace), but you are looking for folder 'flow_speed_10'
2 Comments
More Answers (1)
Fangjun Jiang
on 6 Oct 2022
add "rehash" after the folder is created.
doc rehash
2 Comments
Fangjun Jiang
on 7 Oct 2022
Do you know ".." means the parent folder of the current folder?
Add a breakpoint at the line of "saveas()", run your code and when it is paused, go to Command Window and see if you can successfully run this. If not, figure out the problem.
cd('../data_analysis/CL_over_CD_plot')
See Also
Categories
Find more on Printing and Saving in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!