Note the repetitive nature of the following script. Isn't it possible code this more simply using a loop of some kind, and perhaps the 'eval' command? Especially if you wanted to plot more than six items without changing the code? The 'co60', '4', '6','10','15', etc. correspond to the names of the data files (co60.dat, 4.dat, etc); I was able to create these vectors via the eval command. One difficulty I face is that I seem unable to nest strings within strings: For example, how do you specify 'o' when eval requires string inputs? How do you put a string within a string?
% This plots all 18 lines (3 per energy spectra) on one graph. %figure % hold on % plot(depth,sk_ratio1,'.',depth,sk_ratio2,'o',depth,sk_ratio3,'x', ... % depth,sk_ratio4,'+',depth,sk_ratio5,'*',depth,sk_ratio6,'s') % plot(depth,fitco60_1_plot,'-',depth,fit4_1_plot,'-', ... % depth,fit6_1_plot,'-', depth,fit10_1_plot,'-', ... % depth,fit15_1_plot,'-', depth,fit24_1_plot,'-') % plot(depth,fitco60_2_plot,'--',depth,fit4_2_plot,'--', ... % depth,fit6_2_plot,'--', depth,fit10_2_plot,'--', ... % depth,fit15_2_plot,'--', depth,fit24_2_plot,'--') % title(['S/K vs Depth']) % xlabel(['Depth in cm']) % ylabel('Scerma to Kerma Ratio for Various Energy Spectra') % legend('Co-60','4 MV','6 MV','10 MV','15 MV','24 MV') % This plots the lines for each energy spectra in its own graph in figure. figure subplot(2,3,1) plot(depth,sk_ratio1,'.',depth,fitco60_1_plot,'-',depth,fitco60_2_plot,'--') xlabel('a') legend('Co-60','Linear fit','Quadratic fit') subplot(2,3,2) plot(depth,sk_ratio2,'o',depth,fit4_1_plot,'-',depth,fit4_2_plot,'--') xlabel('b') legend('4 MV','Linear fit','Quadratic fit') subplot(2,3,3) plot(depth,sk_ratio3,'x',depth,fit6_1_plot,'-',depth,fit6_2_plot,'--') xlabel('c') legend('6 MV','Linear fit','Quadratic fit') subplot(2,3,4) plot(depth,sk_ratio4,'+',depth,fit10_1_plot,'-',depth,fit10_2_plot,'--') xlabel('d') legend('10 MV','Linear fit','Quadratic fit') subplot(2,3,5) plot(depth,sk_ratio5,'*',depth,fit15_1_plot,'-',depth,fit15_2_plot,'--') xlabel('e') legend('15 MV','Linear fit','Quadratic fit') subplot(2,3,6) plot(depth,sk_ratio6,'s',depth,fit24_1_plot,'-',depth,fit24_2_plot,'--') xlabel('f') legend('24 MV','Linear fit','Quadratic fit')
figure hold on for i = 1:numberofspectra spectrumname = input('Please input the spectrum''s file name: ','s'); % sk_ratio is calculated; by the way, the spectrum file ends in .dat eval(['sk_ratio' num2str(i) '= s_depth./k_depth;']) dotpos = find(spectrumname == '.', 1, 'last'); % from Walter Roberson if ~isempty(dotpos); spectrumname(dotpos:end) = []; end % Walter Roberson eval(['plot(depth,sk_ratio' num2str(i) ')']) legend(spectrumname) end title(['S/K vs Depth']) xlabel(['Depth in cm']) ylabel('Scerma to Kerma Ratio for Various Energy Spectra')
x1 = rand(1,3); y1 = rand(1,3); x2 = rand(1,3); y2 = rand(1,3); x3 = rand(1,3); y3 = rand(1,3); x4 = rand(1,3); y4 = rand(1,3); figure plot( ... for i = 1:3 eval(['x' num2str(i) 'y' num2str(i) ',']) end x4,y4) ??? for i = 1:3 | Error: Illegal use of reserved keyword "for".
So how do you automate a multiple plot+legend, particularly with unique lines and well-placed legend (if possible)?
in = {1:10, 11:20, '.',3:4,2:3,'*'}; plot(in{:})
Question 2
str = {'A','B'}; legend(str{:})
{sk_ratio01, sk_ratio02; fitco_1_01, fitco_1_02; fitco_2_01, fitco_2_02; 'Co-60' , 'MV 4'; '*' , '+'}
s.('sk_ratio01') = struct('data',data,'fit1',fit1,'fit2',fit2,'marker','*','lbl','Co-60'); s.('sk_ratio02') = struct('data',data,'fit1',fit1,'fit2',fit2,'marker','+','lbl','MV 4');
eval(['spectrum' spectrumname '.energy=' spectrumname])
could be rewritten with dynamic field indexing:
spectrum.(['spn' spectrumname]).energy = spectrumname;
Note that a fieldname cannot start with a number (same rule as for variables applies).
Matlabsolutions.com provides guaranteed satisfaction with a
commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain
experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support
to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been
empanelled after extensive research and quality check.
Matlabsolutions.com provides undivided attention to each Matlab
assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services
include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work
done at the best price in industry.