Extracting data values from function/figure

6 Ansichten (letzte 30 Tage)
Matthew Shiers
Matthew Shiers am 8 Nov. 2011
Beantwortet: Voss am 28 Dez. 2021
Hi,
I've recently started using MATLAB to run through some example functions provided by a book on plasmonic nanoparticles. They offer example codes which can be run to create figures from the book, or edited for research use. This may be a naive question, but if I want to extract a particular set of values (for example, electric field intensity wrt wavelength) into an ASCII file, how do I do this? I normally run the function and produce a figure, an example of one of the codes is below. I basically want to know the exact value at the peak in the graphs, and perhaps at other points. If anyone can help that would be great!
function MakeFig6_5()
% Function to reproduce Fig. 6.5
% Summary of LSP resonances from Mie theory
% for silver spheres in air or water as a function of radius.
%
% This file is part of the SPlaC v1.0 package (copyright 2008)
% Check the README file for further information
%%%%%%%%%%%%%%%%%
% Computations
%%%%%%%%%%%%%%%%%
global noCheckSum;
noCheckSum=true; % no check for numerical problems (much faster)
% noCheckSum=false; % to check for numerical problems with Mie
% wavelengths (column)
lambda=transpose(300:1:800);
% N_{max} for Mie series
nNmax=50;
% wavelength-dependent dielectric function of sphere material
% vector column, same size as lambda [L x 1]
epsilonIn=epsAg(lambda); % silver
% sphere radius in nm (column vector)
aArray=[2;10;15;20;25;30;35;40;50;60;80;100];
nba=length(aArray);
% preallocate arrays
QextResAir=0*aArray;
indResAir=0*aArray;
QextResWat=0*aArray;
indResWat=0*aArray;
nNbLambda=length(lambda);
indForMaxAir=transpose(1:nNbLambda);
indForMaxWat=indForMaxAir;
% loop through each of the radii
for nn=1:nba
% dielectric constant of outside medium (real positive)
epsilonM=1.0; % air first
a=aArray(nn);
% Solve problem using Mie theory
stM=PweSolveSingleSphere(nNmax,a,lambda,epsilonM,epsilonIn,'noplot');
% Look for maximum of Q_{ext} that is redshifted compared to previous
% radius (to isolate dipolar LSP rather than multipolar)
% the zeros are added to make sure the vector in max is same length as
% lambda
[QextResAir(nn),indResAir(nn)]=max([zeros(nNbLambda-length(indForMaxAir),1); stM.Qext(indForMaxAir)]);
indForMaxAir=transpose(indResAir(nn):nNbLambda);
% repeat for water
epsilonM=1.77; % water
stM=PweSolveSingleSphere(nNmax,a,lambda,epsilonM,epsilonIn,'noplot');
% Look for maximum of Q_{ext}
[QextResWat(nn),indResWat(nn)]=max([zeros(nNbLambda-length(indForMaxWat),1); stM.Qext(indForMaxWat)]);
indForMaxWat=transpose(indResWat(nn):nNbLambda);
end
%%%%%%%%%%%%%%%%%
% Drawing
%%%%%%%%%%%%%%%%%
% create, position, and resize figure
scrsz = get(0,'ScreenSize'); % scrsz(3) contains screen width
figAspectRatio=1.5;
figWidth=3/4*scrsz(3);
figure('Name','Fig. 6.5', ...
'Position',[(scrsz(3)-figWidth)/2 scrsz(4)-150-figWidth/figAspectRatio figWidth figWidth/figAspectRatio]);
% plot results
ax1=axes('XAxisLocation','bottom','Xcolor','k', ...
'YAxisLocation','left','Ycolor','b', ...
'XLim',[0 118],'YLim',[302 800]);
set(get(ax1,'XLabel'),'String','Radius, a [nm]');
set(get(ax1,'YLabel'),'String','Dipolar LSPR wavelength [nm]');
line(aArray,lambda(indResAir),'Color','b','Marker','s','LineStyle','-');
line(aArray,lambda(indResWat),'Color','b','Marker','o','LineStyle','-');
legend({'\lambda_{Res} (air)','\lambda_{Res} (water)'},'Location','NorthEast');
title('Summary of dipolar LSP resonance wavelengths for silver spheres');
axes('Position',get(ax1,'Position'),...
'XAxisLocation','top','XColor','k', ...
'YAxisLocation','right','Ycolor','r', ...
'Color','none', ...
'YScale','linear', ...
'YLim',[1.8 23.8],'XLim',[0 118], ...
'XTickLabel',[]);
line(aArray,QextResAir,'Color','r','LineStyle','--','Marker','s');
line(aArray,-real(epsilonIn(indResAir)),'Color','m','LineStyle',':','Marker','s');
legend({'Q_{ext}(\lambda_{Res}) (air)','-Re(\epsilon(\lambda_{Res})) (air)'},'Location','SouthEast','Color','w');
  1 Kommentar
Jan
Jan am 8 Nov. 2011
Please apply a proper code formatting as explained in the "Markup help" link on this page.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Voss
Voss am 28 Dez. 2021
You can use fopen() to open a text file for writing, fprintf() to write your data to the file with the format you want, and fclose() to close the file after the writing is done.
Alternatively, you can look into writematrix() to see if that could be of use.

Kategorien

Mehr zu Data Import from MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by