figure saving in created folder. Error using savepath: invalid or missing path
이전 댓글 표시
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
댓글 수: 8
Walter Roberson
2022년 10월 7일
What is the total length of filename with all directories included?
Andrea Giordano
2022년 10월 7일
Walter Roberson
2022년 10월 7일
That is a relative file name. If you expand the name out completely with the C:\ all the way to the end, then what is length() of the resulting name?
Andrea Giordano
2022년 10월 7일
Walter Roberson
2022년 10월 7일
could I ask you to experiment by changing the # character to _ (underscore)?
Andrea Giordano
2022년 10월 7일
Walter Roberson
2022년 10월 7일
Please run this test code:
if ~isdir('..')
error('Corrupt or very very old file system, missing .. directory entry')
elseif ~isdir('../data_analysis')
error('No folder ../data_analysis')
elseif ~isdir('../data_analysis/CD_plot')
error('No folder ../data_analysis/CD_plot')
elseif ~isdir('../data_analysis/CD_plot/flow_speed_10')
error('No folder ../data_analysis/CD_plot/flow_speed_10')
else
fprintf('folder path ../data_analysis/CD_plot/flow_speed_10 is okay')
end
Andrea Giordano
2022년 10월 10일
채택된 답변
추가 답변 (1개)
Fangjun Jiang
2022년 10월 6일
0 개 추천
add "rehash" after the folder is created.
doc rehash
댓글 수: 2
Andrea Giordano
2022년 10월 7일
Fangjun Jiang
2022년 10월 7일
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')
카테고리
도움말 센터 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!