How to open .m files in octave on OSX 10.6.8 - octave

This file ex1.m, I "open with" octave, but it doesn't do anything. the first few lines of code are....
%% Machine Learning Online Class - Exercise 1: Linear Regression
% Instructions
% ------------
%
% This file contains code that helps you get started on the
% linear exercise. You will need to complete the following functions
% in this exericse:
%
% warmUpExercise.m
% plotData.m
% gradientDescent.m
% computeCost.m
% gradientDescentMulti.m
% computeCostMulti.m
% featureNormalize.m
% normalEqn.m
%
% For this exercise, you will not need to change any code in this file,
% or any other files other than those mentioned above.
%
% x refers to the population size in 10,000s
% y refers to the profit in $10,000s
%
%% Initialization
clear ; close all; clc
%% ==================== Part 1: Basic Function ====================
% Complete warmUpExercise.m
fprintf('Running warmUpExercise ... \n');
fprintf('5x5 Identity Matrix: \n');
warmUpExercise()
fprintf('Program paused. Press enter to continue.\n');
pause;
etc.... There were more files, I'm not sure how to get this to run...

This looks like part of Andrew Ng's Online Machine Learning class offered from www.coursera.org You might have more luck asking these questions in the forums at the class website after attempting to follow the tutorials yourself.
You need to have all of the relevant files in the directory first. Then actually complete the assignment. Most of those assignments do nothing until you complete the code section.
This file contains code that helps you get started on the linear exercise. You will need to complete the following functions in this exericse:
warmUpExercise.m
plotData.m
gradientDescent.m
computeCost.m
gradientDescentMulti.m
computeCostMulti.m
featureNormalize.m
normalEqn.m
After you are done, open a mac Terminal and cd to the directory where ex1.m is located. You can run the program by starting octave and entering the name of the file without the .m extension. Type ex1. Alternatively, you could type octave -q ex1.m from the terminal if you don't want to be in the octave shell after the program is done running.

Related

Can you set an artificial starting point in your code in Octave?

I'm relatively new to using Octave. I'm working on a project that requires me to collect the RGB values of all the pixels in a particular image and compare them to a list of other values. This is a time-consuming process that takes about half a minute to run. As I make edits to my code and test it, I find it annoying that I need to wait for 30 seconds to see if my updates work or not. Is there a way where I can run the code once at first to load the data I need and then set up an artificial starting point so that when I rerun the code (or input something into the command window) it only runs a desired section (the section after the time-consuming part) leaving the untouched data intact?
You may set your variable to save into a global variable,
and then use clear -v instead of clear all.
clear all is a kind of atomic bomb, loved by many users. I have never understood why. Hopefully, it does not close the session: Still some job for quit() ;-)
To illustrate the proposed solution:
>> a = rand(1,3)
a =
0.776777 0.042049 0.221082
>> global a
>> clear -v
>> a
error: 'a' undefined near line 1, column 1
>> global a
>> a
a =
0.776777 0.042049 0.221082
Octave works in an interactive session. If you run your script in a new Octave session each time, you will have to re-compute all your values each time. But you can also start Octave and then run your script at the interactive terminal. At the end of the script, the workspace will contain all the variables your script used. You can type individual statements at the interactive terminal prompt, which use and modify these variables, just like running a script one line at the time.
You can also set breakpoints. You can set a breakpoint at any point in your script, then run your script. The script will run until the breakpoint, then the interactive terminal will become active and you can work with the variables as they are at that point.
If you don't like the interactive stuff, you can also write a script this way:
clear
if 1
% Section 1
% ... do some computations here
save my_data
else
load my_data
end
% Section 2
% ... do some more computations here
When you run the script, Section 1 will be run, and the results saved to file. Now change the 1 to 0, and then run the script again. This time, Section 1 will be skipped, and the previously saved variables will be loaded.

Make .oct files for distmesh

Distmesh is a popular triangulation routine written by Perrson in matlab. In some cases Persson uses .mex files to speed up the calculations. I want to extend this to octave. I have read https://enacit1.epfl.ch/octave_doc/octave/octave.html/Getting-Started-with-Oct_002dFiles.html and other tutorials and have succesfully made those examples work. Here is a typivcal code from distmesh:
function d=dpoly(p,pv)
% Copyright (C) 2004-2012 Per-Olof Persson. See COPYRIGHT.TXT for details.
np=size(p,1);
nvs=size(pv,1)-1;
ds=dsegment(p,pv);
%ds=zeros(np,nvs);
%for iv=1:nvs
% ds(:,iv)=donesegment(p,pv(iv:iv+1,:));
%end
d=min(ds,[],2);
d=(-1).^(inpolygon(p(:,1),p(:,2),pv(:,1),pv(:,2))).*d;
% MEXED
%function ds=donesegment(p,pv)
%
%e=ones(size(p,1),1);
%
%v=diff(pv,1);
%w=p-e*pv(1,:);
%
%c1=sum(w.*v(e,:),2);
%c2=sum(v(e,:).^2,2);
%
%ds=0*e;
%
%ix=c1<=0;
%ds(ix)=sqrt(sum((p(ix,:)-pv(1*ones(sum(ix),1),:)).^2,2));
%
%ix=c1>=c2;
%ds(ix)=sqrt(sum((p(ix,:)-pv(2*ones(sum(ix),1),:)).^2,2));
%
%ix=c1>0 & c2>c1;
%nix=sum(ix);
%if nix>0
% Pb=ones(nix,1)*pv(1,:)+c1(ix)./c2(ix)*v;
% ds(ix)=sqrt(sum((p(ix,:)-Pb).^2,2));
%end
The task in to convert function d=donesegment(p,v) (everything beneath the line % MEXED) into a .oct file. I am neither a c or cpp programmer, so I don't know what headers need to be includesd, or the statements needed to include them.
John Burkhardt's site has a copy of distmesh that already contains pure-matlab versions of the routines that are written in C++. Specifically dsegment.m is available there. I have run distmesh in octave using that file; it is a little slow but acceptable for small-to-medium size cases.

Publishing a report including a relative path to an image file via `publish`

I've read, tested this documentation and this example and tried on my own, but have not yet found a solution for the described issue.
According to the documentation, either the image file is on the same path of the used MATLAB script, in which case one could easily add it as described
% <<Filename.png>>
or one needs to add the full path:
% <<C:\\Fullpath\Filename.png>>
otherwise one would get an empty document (at least when publish is creating .html files).
As the documented code should be shared I need a relative path to avoid later issues.
Is there any solution or workaround within MATLAB?
Some of my former attempts:
% <<.\Filename.png>> # with and without "" or ''
% <<..\Filename.png>> # with and without "" or ''
% <<..\..\Filename.png>> # with and without "" or ''
The image does not appear.
Assuming this directory structure:
This code,
function q51539865
%% Image From URL
%
% <<https://i.stack.imgur.com/zSPip.jpg>>
%
%% Image From Relative Path
%
% <<../InnerPath/zSPip.jpg>>
%
Produces the following web page:
When inspecting the 2nd image we see:

Setting Octave interpreter to 'tex'

Such a simple question to have wasted an hour or two of my time. The Octave docs allude to setting the interpreter to tex and never say how to do it. I've looked on line and through stackoverflow and haven't found how to do this. I've also looked at the .octaverc files and have seen nothing that would indicate how to turn on the tex edit function. I am using Debian GNUOctave version 4.0.0. Please help.
Gary Roach
The interpreter property is set to "tex" per default for axes, line, text, patch and surface. So changing interpreter makes only sense if you want to switch to "none":
set (findobj (gcf, "-property", "interpreter"), "interpreter", "none")
This sets "interpreter"="none" for al children of the current figure.
If you want to have some fancy latex stuff in your plots and not only simple tex commands you can render it with latex:
close all
graphics_toolkit fltk
sombrero ();
title ("The sombrero function:")
fcn = "$z = \\frac{\\sin\\left(\\sqrt{x^2 + y^2}\\right)}{\\sqrt{x^2 + y^2}}$";
text (0.5, -10, 1.8, fcn, "fontsize", 20);
print -depslatexstandalone sombrero
## process generated files with pdflatex
system ("latex sombrero.tex");
## dvi to ps
system ("dvips sombrero.dvi");
## convert to png
system ("gs -dNOPAUSE -dBATCH -dSAFER -sDEVICE=png16m -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r100x100 -dEPSCrop -sOutputFile=sombrero.png sombrero.ps")
which gives:

Creating Function in Octave

I'm beginning with octave. I've created a file called squareThisNumber.m in My Documents with the following code:
function y = squareThisNumber(x)
y = x^2;
I set the directory to look at My Documents with
cd 'C:\Users\XXXX\My Documents'
I type "squareThisNumber(3)" into octave, and all I'm getting is "Error: 'squareThisNumber' undefined near line 3 column 1." What am I doing wrong?
EDIT:
When I type ls into octave, I get "error: ls: command exited abnormally with status 127". Did I not install Octave correctly?
This behavior sure does seem like there's a problem with octave's current working directory. Does the command dir or pwd also have the same problem?
But you might be able to ignore all of that by
addpath("C:\Users\XXXX\My Documents");
Did you place the end keyword at the end? Code example below works perfectly for me
https://saturnapi.com/fullstack/function-example
% Welcome to Saturn's MATLAB-Octave API.
% Delete the sample code below these comments and write your own!
function y = squareThisNumber(x)
y = x^2;
end
squareThisNumber(9)