matlab - is there a function which echo text of file ? - matlab

Since it is not possible to have a script and a function definition in the same file , I thought to echo the function which I want to attach in the script such that I get a script with function code and then some usage with this function .
For example -
func1.m
function [result] = func1(x)
result=sqrt(x) ;
end
script1.m
echo(func1.m) ;
display(func1(9))
Desire output for script1.m
function [result] = func1(x)
result=sqrt(x) ;
end
display(func1(9))
3
Have you any idea for that ?

Since a convoluted solution was already proposed, why not stating the obvious?
Matlab has a built-in command that does exactly what you want. It is called type:
>> type('mean')
will give you this:
function y = mean(x,dim)
%MEAN Average or mean value.
% For vectors, MEAN(X) is the mean value of the elements in X. For
% matrices, MEAN(X) is a row vector containing the mean value of
% each column. For N-D arrays, MEAN(X) is the mean value of the
% elements along the first non-singleton dimension of X.
%
% MEAN(X,DIM) takes the mean along the dimension DIM of X.
%
% Example: If X = [0 1 2
% 3 4 5]
%
% then mean(X,1) is [1.5 2.5 3.5] and mean(X,2) is [1
% 4]
%
% Class support for input X:
% float: double, single
%
% See also MEDIAN, STD, MIN, MAX, VAR, COV, MODE.
% Copyright 1984-2005 The MathWorks, Inc.
% $Revision: 5.17.4.3 $ $Date: 2005/05/31 16:30:46 $
if nargin==1,
% Determine which dimension SUM will use
dim = min(find(size(x)~=1));
if isempty(dim), dim = 1; end
y = sum(x)/size(x,dim);
else
y = sum(x,dim)/size(x,dim);
end

You could use this:
function echo(mfile)
filename=which(mfile);
if isempty(filename)
fprintf('Invalid input - check you are inputting a string.');
return;
end
fid=fopen(filename,'r');
if (fid<0)
fprintf('Couldn''t open file.');
end
file=fread(fid,Inf);
fclose(fid);
fprintf('%s',file);
end
This will open a file, read it, and print it. Note that you need to provide the input as a string, i.e. with single quotes around it, and need to have '.m' at the end:
echo('fread.m')
Not
echo(fread.m) % This won't work
echo('fread') % This won't work

Just for completeness, there's also dbtype which prepends line numbers.

Related

Input parser: one parameter dependent from another parameter

I am using the input parser functionality a lot but now I am facing the following issue:
I am coding an object that computes the rate-of-change of time series, i.e. one input argument is a matrix of doubles (timeSeries). A second input parameter is the time period used to compute the rate-of-change (lag). lag can be a vector wherby different rate-of-changes will be computed. A third input vector is called weightVector. weightVector will be used to compute an average of all rate-of-changes and applies the appropriate weights to the respective rate-of-change results.
Now, I always like to define some default values when using input parser. I would like to define weightVector to be an equal weighted vector. However, the length of the default weightVector is dependent on the length of lag. For example, if lag = [1,2,3], then weightVectorshould equal [1/3, 1/3, 1/3]. How am I supposed to code a situation like this? My current code for the constructor looks like this:
function obj = roc(timeSeries, varargin)
%% Input parser
% 1. Create input parser instance
p = inputParser;
% 2. Default values for input arguments
default_lag = 1;
default_weightVector = 1/length(lag); % This line is causing
% problems as LAG isn't
% defined, yet.
% 3. Validation of input arguments
valid_lag = {'vector', 'nonempty', 'integer', 'positive'};
check_lag = #(x) validateattributes(x, {'numeric'}, valid_lag);
valid_weightVector = {'vector', 'nonempty'};
check_weightVector = #(x) validateattributes(x, {'numeric'}, ...
valid_weightVector);
% 4. Add input arguments to input scheme
p.addRequired('timeSeries');
p.addParamater('lag', default_lag, check_lag);
p.addParameter('weightVector', default_weightVector, check_weightVector);
% 5. Parse input arguments
parse(p, timeSeries, varargin{:});
% 6. Assign results to variables
lag = p.Results.lag;
weightVector = p.Results.weightVector;
%% Main code
end % Constructor
function obj = roc(timeSeries, varargin)
%% Input parser
% 1. Create input parser instance
p = inputParser;
% 2. Default values for input arguments
default_lag = 1;
default_weightVector = 1;
% 3. Validation of input arguments
valid_lag = {'vector', 'nonempty', 'integer', 'positive'};
check_lag = #(x) validateattributes(x, {'numeric'}, valid_lag);
% 4. Add input arguments to input scheme
p.addRequired('timeSeries');
p.addParameter('lag', default_lag, check_lag);
p.addParameter('weightVector', default_weightVector);
% 5. Parse input arguments
parse(p, timeSeries, varargin{:});
% 6. Assign results to variables
lag = p.Results.lag;
if check_weightVector(p.Results.weightVector, lag) == true
weightVector = p.Results.weightVector;
end
function vout = check_weightVector(weightVector, lag) % validation function
if length(lag) ~= length(weightVector)
error('lengthWeightVector:WrongNumberOfElements', 'The number of elements in "weightVector" must correspond to the number of elements in "lag"');
elseif sum(weightVector) ~= 1
error('sumWeightVector:SumNotEqualToOne', 'The sum of elements in "weightVector" must equal 1');
end
vout = true;
end
%% Main code
end

In Matlab I keep getting undefined variable or function error despite clearly defining the variable

I have this piece of code in Matlab which should take an Airfoil profile and increase the number of points so that when I plot the profile in another programme I will get a smoother curve.
clear
%reading an external data file
fid = fopen('NACA0015.txt');
a = fscanf(fid,'%g %g',[2 inf]); % It has two rows now.
a = a'; % matrix transpose
n = input('200') %e.g., n=35
for i=1:n
for j=1:2
fprintf('%12.7f',a(i,j)); %a(i,1) is first column, a(i,2) is 2nd col
end
fprintf('\n');
end
fclose(fid);
for i=1:n
x(i)=a(i,1); %x , y vectors
y(i)=a(i,2);
end
% use spline to create more points
xx=0:0.01:1 % e.g., step =0.01 (number of points = (1-0)/0.01=100)
yy = spline(x,y,xx); % xx and yy are new generated values of Naca0012
fprintf('\n print spline values \n');
plot(xx,yy,'ro')
hold on
plot(x,y,'*')
When I run this I get the error
Undefined function or variable 'x'.
Error in reading_external_data_and_spline (line 26)
yy = spline(x,y,xx); % xx and yy are new generated values of Naca0012
I am at a complete loss as to why this is not working when the x variable is clearly defined in the code, please could someone help me with this
It's how you're using input. The argument in input isn't the default value, it's the prompt text. If you type the command into the console and hit enter, you get this:
>> n = input('200')
200
n =
[]
>>
Input doesn't accept a default. If you really want to have an interactive prompt with a default answer, you want inputdlg:
answer = inputdlg('Enter a number of lines to parse', 'n', 1, '200');
n = str2double(answer);
note that inputdlg returns text always, so you need to convert to a number.

plotting a graph by calling a function

M = round(csvread('noob.csv'))
save projectDAT.dat M -ascii
load projectDAT.dat
mat = (projectDAT)
sum_of_rows(mat)
plotthegraph
This is my main script. I have a excel file, which opens up a 20 x 20 matrix in matlab. Now I have to call a function in this mainscript, which would find the sum of elements in a row for me and put them in a column vector. Here is my function:
function sumRow = sum_of_rows(mat)
[m n] = size(mat);
sumRow = zeros(m,1);
for i = 1:m;
for j = 1:n;
sumRow(i) = sumRow(i) + mat(i,j);
end
end
vec = sumRow;
end
I am required to plot a line graph using this column vector. I am supposed to call a function from the mainscript. The function should be able to take the input from this sum_of_rows function. I tried doing this:
function plotthegraph(~)
% Application for plotting the height of students
choice = menu('Choose the type of graph', 'Plot the data using a line plot', 'Plot the data using a bar plot');
if choice == 1
plot_line(sum_of_rows)
y = sum_of_rows
x = 1:length(y)
plot(x,y)
title('Bar graph')
xlabel('Number of characters')
ylabel('Number of grades')
elseif choice == 2
plot_bar(sum_of_columns)
end
Its not working out though. Can someone please help me out, I would really appreciate it. Thank you.
You could do the following to get rid of the sum_of_rows function:
sumRow = sum(mat,2);
The second argument tells the sum to add all the columns in the matrix rowwise, giving you the sum of each row in mat.
To plot this vector you need to pass it as input to your function. Also matlab takes care of the x values for you if you do not specify variable along, doing exactly what you did manually while defining x. Your function would look like this:
function plotthegraph(sum_of_rows)
% Application for plotting the height of students
prompt='Type 1 for line plot, 2 for bar';
choice=input(prompt)
if choice == 1
plot(sum_of_rows)
title('Line graph')
xlabel('Number of characters')
ylabel('Number of grades')
elseif choice == 2
bar(sum_of_rows)
end
end
So you'd have to call this function passing the sum of rows:
plotthegraph(sumRow)

program isnt working because of an error in y

this is my program, its supposed to get an input from the user for the starting x value, stepping value, and an ending x value, the user also inputs the y function he likes, the graph is then supposed to graph it but i keep getting an error, any ideas?
clear all ; % clear memory
close all ; % close any open figures
drawnow ; % update screen now
clc ; % clear screen
display('** Welcome to Plotting Program **') ;
display(' ');
start=input('please enter starting x value:');
step=input('please enter ending x value:');
stop=input('please enter step value:');
y= double(input('please input your equation:'));
x=double(start:step:stop);
plot(x,y);
You can use str2func() to convert string input into a function handle:
y = input('Input only the RHS of your equation as a function of ''x'' and enclose in '': ')
y = str2func(['#(x)' y]);
plot(x,y(x))
Also note that double(start:step:stop) will not work, since you are converting chars to their ASCII mapping:
double('20')
ans =
50 48
Use instead str2double(input('...'))

How do I display strings and numbers together in MATLAB?

I have a 500-by-1 matrix of strings S and a 500-by-1 matrix of numbers N. I want to see them in the variable editor together like this:
S(1) N(1)
S(2) N(2)
...
S(500) N(500)
Is this possible?
The following should allow you to look at the variables together in the Command Window:
disp([char(S) blanks(numel(N))' num2str(N)]);
The array S (which I presume is a cell array) is converted to a character array using the function CHAR. It's then concatenated with a column vector of blanks (made using the function BLANKS) and then a string representation of the numeric array N (made using the function NUM2STR). This is then displayed using the function DISP.
Speaking narrowly to your question, just convert the numbers to cells. You'll have a single variable that the array editor can handle.
X = [ S num2cell(N) ];
More broadly, here's an array-oriented variant of sprintf that's useful for displaying records constructed from parallel arrays. You'd call it like this. I use something like this for displaying tabular data a lot.
sprintf2('%-*s %8g', max(cellfun('prodofsize',S)), S, N)
Here's the function.
function out = sprintf2(fmt, varargin)
%SPRINTF2 Quasi-"vectorized" sprintf
%
% out = sprintf2(fmt, varargin)
%
% Like sprintf, but takes arrays of arguments and returns cellstr. This
% lets you do formatted output on nonscalar arrays.
%
% Example:
% food = {'wine','cheese','fancy bread'};
% price = [10 6.38 8.5];
% sprintf2('%-12s %6.2f', food, price)
% % Fancier formatting with width detection
% sprintf2('%-*s %6.2f', max(cellfun('prodofsize',food)), food, price)
[args,n] = promote(varargin);
out = cell(n,1);
for i = 1:n
argsi = grab(args, i);
out{i} = sprintf(fmt, argsi{:});
end
% Convenience HACK for display to command line
if nargout == 0
disp(char(out));
clear out;
end
function [args,n] = promote(args)
%PROMOTE Munge inputs to get cellstrs
for i = 1:numel(args)
if ischar(args{i})
args{i} = cellstr(args{i});
end
end
n = cellfun('prodofsize', args);
if numel(unique(n(n > 1))) > 1
error('Inconsistent lengths in nonscalar inputs');
end
n = max(n);
function out = grab(args, k)
%GRAB Get the kth element of each arg, popping out cells
for i = 1:numel(args)
if isscalar(args{i})
% "Scalar expansion" case
if iscell(args{i})
out{i} = args{i}{1};
else
out{i} = args{i};
end
else
% General case - kth element of array
if iscell(args{i})
out{i} = args{i}{k};
else
out{i} = args{i}(k);
end
end
end