Is there any function in MATLAB that determine free disk space? I have made a temporal function that uses MS-DOS dir command and parses the last line of its output. I think it's working as expected but I guess (1) it won't work in other systems (OS X, Linux, Unix, etx.) and (2) can also fail in different Windows versions. Perhaps someone could improve it to make it more generic? Thanks
The code:
function out = freediskspace
[~,d] = dos('dir');
C = textscan(d,'%s','Delimiter','\n'); C = C{1}{end};
C = strrep(C,',','');
r = regexp(C,'\d+','match');
out = str2double(r{2});
end
You can use a Java call (this works on both Linux and Windows - I have not checked OSX but it should be fine).
function free = getFreeSpace(path)
if nargin < 1 || isempty(path)
path= '.';
end
free = java.io.File(path).getFreeSpace();
end
For example,
>> f = getFreeSpace('C:\')
f =
3.9338e+11
Related
I'm trying to run a programme coded in Psychtoolbox-3 that should register a keypress. But when I run it, even just this section from the command window, it doesn't respond to the E, P keys (or any) and I have to stop the operation using Ctrl-C. I have tried changing it to {e, p} (which are the names I found using KbName('KeyNames')), but it doesn't work.
The same code works on my supervisor's computer - I am using a Mac with OS 11.1.
KbName('UnifyKeyNames');
keyresp = KbName({'E','P'});
key = 0;
while ~key
[key,tkey] = CheckKeyPress(keyresp);
end
The CheckKeyPress is this function (and it works - gives output 0):
function [key,tkey] = CheckKeyPress(whichkeys)
if nargin < 1 || isempty(whichkeys)
whichkeys = 1:256;
end
key = 0;
[iskeydown,tkey,keys] = KbCheck(-1);
if any(keys(whichkeys))
key = find(keys(whichkeys),1);
end
end
I have also tried looking at PsychHID('Devices') and my keyboard is there (and no other keyboards).
Thanks for any help!
Solved! It was a simple mac problem :)
After I tried KbQueueCreate and got an error message I found the same one on another thread - the only problem is I had to allow Matlab to access keyboard input on my laptop.
Settings - Security and Privacy - Input Monitoring
It is always a big pain in MacOs. I use this code:
close all
clear all
clc
ListenChar(0);
Devices=PsychHID('Devices');
keyboardsIDs = [];
for iiD = 1:numel(Devices)
try
KbQueueCreate(Devices(iiD).index);
KbQueueStart(Devices(iiD).index);
keyboardsIDs(end+1,1) = Devices(iiD).index;
end
end
stopScript = 0;
while ~stopScript
for iiD = 1:numel(keyboardsIDs)
[keyIsDown, firstPress]=KbQueueCheck(keyboardsIDs(iiD));
if keyIsDown
keyID = find(firstPress)
disp(keyID)
if any(keyID ==20), stopScript =1; end
end
end
end
for iiD = 1:numel(keyboardsIDs)
KbQueueStop(keyboardsIDs(iiD));
end
ListenChar();
UPDATE
This is a bit dirty workaround, but it will works in any situation.
Press 'q' to exit the loop
I get the following error trying to solve a non linear system in Octave:
error: #Jfun: no function and no method found
error: called from
voc at line 4 column 13
I'm using 4 scripts and I couldn't find the source of the error. The ffun, jfun and newtonsys files have been tested before and I am almost sure the issue is not there (I don't know if it could be an issue with the naming of variables though), but I have included them all below just in case.
file voc.m
x0=[9;8;0.5];
tol=10^-3;
nmax=1000;
[z,res,niter]=newtonsys(#Ffun,#Jfun,x0,tol,nmax)
File Ffun.m
q=1.602E-19;
k=1.381E-23;
Ncs=12;
Tc=329.25;
gamma=1.35;
Isc=9.14;
Rsh=94.5;
Vmp=37.8;
Imp=8.74;
function F=Ffun(x)
F(1,1)=Isc+x(2)*[exp((q*Isc*x(3))/(gamma*k*Tc*Ncs))-1]-(Isc*x(3))/Rsh-x(1);
F(2,1)=x(2)*[exp(q*(Voc)/(gamma*k*Tc*Ncs))-1]+(Voc/Rsh)-x(1);
F(3,1)=Imp+x(2)*[exp(q*(Vmp+Imp*x(3))/(gamma*k*Tc*Ncs))-1]+(Vmp+(Imp*x(3)))/Rsh-x(1);
endfunction
File JFun.m
q=1.602E-19;
k=1.381E-23;
Ncs=12;
Tc=329.25;
gamma=1.35;
Isc=9.14;
Rsh=94.5;
Vmp=37.8;
Imp=8.74;
function J=Jfun(x)
J(1,1)=-1;
J(1,2)=exp((q*Isc*x(3))/(gamma*k*Tc*Ncs))-1;
J(1,3)=x(2)*[exp((q*Isc*x(3))/(gamma*k*Tc*Ncs))]*(q*Isc/(gamma*k*Tc*Ncs))-(Isc/Rsh);
J(2,1)=-1;
J(2,2)=exp(q*(Voc)/(gamma*k*Tc*Ncs))-1;
J(2,3)=0;
J(3,1)=-1;
J(3,2)=exp(q*(Vmp+Imp*x(3))/(gamma*k*Tc*Ncs))-1;
J(3,3)=x(2)*[exp(q*(Vmp+Imp*x(3))/(gamma*k*Tc*Ncs))]*(q*Imp/(gamma*k*Tc*Ncs))+(Imp/Rsh);
endfunction
file newtonsys.m
function [x,res,niter] = newtonsys(Ffun,Jfun,x0,tol,...
nmax, varargin)
niter = 0;
err = tol + 1;
x = x0;
while err >= tol & niter < nmax
J = Jfun(x,varargin{:});
F = Ffun(x,varargin{:});
delta = - J\F;
x = x + delta;
err = norm(delta);
niter = niter + 1;
end
res = norm(Ffun(x,varargin{:}));
if (niter==nmax & err> tol)
fprintf(['Il metodo non converge nel massimo ',...
'numero di iterazioni. L''ultima iterata\n',...
'calcolata ha residuo relativo pari a %e\n'],F);
else
fprintf(['Il metodo converge in %i iterazioni',...
' con un residuo pari a %e\n'],niter,F);
end
return
The problem is your JFun.m file is NOT a function file, it is a script file which happens to define an 'on-the-spot' function JFun within it. If the voc.m script happens to call that function before it has been defined (i.e. before the JFun.m script has had a chance to be run and therefore end up defining that function in the current environment) then it will complain it's not there.
The solution in your case is to move all those variable definitions inside the function block, making it a proper 'function file', which will then be accessible from voc (as long as it's on the same directory / in the octave path).
Alternatively, if you still prefer JFun.m to be a script, (e.g. maybe you do want all those variables to end up being defined at global scope), then simply make sure you run it as a script first, such that it first defines the function you need; however, in that case, it is a good idea to change the name of your script to something else, so that its name doesn't conflict with the on-the-spot function defined inside it.
Have a quick look at the respective section in the manual, and in particular this part.
I've had a look around and can't quite seem to get a grasp of is going on with this. I'm using R in Eclipse. The file I'm trying to import is 700mb with around 15mil rows and 6 columns. As I was having problems loading in I have started using the ff package.
library(ff)
FDF = read.csv.ffdf(file='C:\\Users\\William\\Desktop\\R Data\\GBPUSD.1986.2014.txt', header = FALSE, colClasses=c('factor','factor','numeric','numeric','numeric','numeric'), sep=',')
names(FDF)= c('Date','Time','Open','High','Low','Close')
#names the columns in the ffdf file
dim(FDF)
# produces dimensions of the file
I then want to create a POSIXct sequence which will later be joined against the imported file. I had tried;
tm1 = seq(as.POSIXct("1986/12/1 00:00"), as.POSIXct("2014/09/04 23:59"),"mins"))
tm1 = data.frame (DateTime=strftime(tm1,format='%Y.%m.%d %H:%M'))
However R kept of crashing. I then tested this is RStudio and saw that their where constraints on the vector. It did, however, produce the correct
dim(tm1)
names(tm1)
So I went back into Eclipse thinking this was something to do with memory allocation. I've attempted the following;
library(ff)
tm1 = as.ffdf(seq(as.POSIXct("1986/12/1 00:00"), as.POSIXct("2014/09/04 23:59"),"mins"))
tm1 = as.ffdf(DateTime=strftime(tm1,format='%Y.%m.%d %H:%M'))
names(tm1) = c('DateTime')
dim(tm1)
names(tm1)
This gives an error of
no applicable method for 'as.ffdf' applied to an object of class "c('POSIXct', 'POSIXt')"
I can't seem to work around this. I then tried ...
library(ff)
tm1 = as.ff(seq(as.POSIXct("1986/12/1 00:00"), as.POSIXct("2014/09/04 23:59"),"mins"))
tm1 = as.ff(DateTime=strftime(tm1,format='%Y.%m.%d %H:%M'))
Which produce the output dates, however not in the correct format. In addition to this, when ...
dim(tm1)
names(tm1)
where executed they both returned null.
Question
How can I produce a POSIXct seq in the format I require above?
We'll we got there in the end.
I believe the problem was the available RAM during the creation of the full vector. As this was the case I broke the vector into 3, converted them into ffdf format to free up RAM and then used rbind to bind them together.
The problem with formatting the vector once created, I believe, was due to accessing RAM. Every time I tried this R crashed.
Even with the work around below my machine is slowing (4gb). I've ordered some more RAM and hope this will smooth future operations.
Below is the working code;
library(ff)
library(ffbase)
tm1 = seq(from = as.POSIXct('1986-12-01 00:00'), to = as.POSIXct('2000-12-01 23:59'), by = 'min')
tm1 = data.frame(DateTime=strftime(tm1, format='%Y.%m.%d %H:%M'))
# create data frame within memory contrainst
tm1 = as.ffdf(tm1)
# converts to ffdf format
memory.size()
tm2 = seq(from = as.POSIXct('2000-12-02 00:00'), to = as.POSIXct('2010-12-01 23:59'), by = 'min')
tm2 = data.frame(DateTime=strftime(tm2, format='%Y.%m.%d %H:%M'))
# create data frame within memory contrainst
tm2 = as.ffdf(tm2)
memory.size()
tm3 = seq(from = as.POSIXct('2010-12-2 00:00'), to = as.POSIXct('2014-09-04 23:59'), by = 'min')
tm3 = data.frame(DateTime=strftime(tm3, format='%Y.%m.%d %H:%M'))
memory.size()
tm3 = as.ffdf(tm3)
# converts to ffdf format
memory.size()
tm4 = rbind(tm1, tm2, tm3)
# binds ffdf objects into one
dim(tm4)
# checks the row numbers
I stumbled across this problem while working with Jacket.
I use a compiled function (compiled with gcompile) within a gfor loop. This is meant to be supported as far as I know: http://wiki.accelereyes.com/wiki/index.php/GCOMPILE
But I observed that while the uncompiled function delivers the correct results, the compiled function gives the same output for all the gfor-iterations:
%================
% function[C] = test(A,B)
% C = A+B;
% end
%================
testing = gcompile('test.m');
A = gdouble(1:1:10);
B = gdouble(2:2:20);
C1 = gzeros(10,1);
C2 = gzeros(10,1);
gfor l=1:10
C1(l) = test(A(l),B(l));
C2(l) = testing(A(l),B(l));
gend
The output is:
C1 = [ 3,6,9,12,15,18,21,24,27,30]
(correct result)
C2 = [ 3,3,3,3,3,3,3,3,3,3]
Can you verify/rebut my results?
What am I doing wrong?
Cheers,
Angela
I was able to reproduce this behavior by running Jacket on MATLAB. It appears that gcompile does not work over GFOR as it should, and the documentation was in error. Sorry about that.
how to write a program in matlab that reads a certain number of images let's say 20 for example which are saved in a given directory (C:) such that later i can use them. suppose that the images are saved by numbers. later, i am gonna use them.
I'd have the code look something like this. Assuming cell array im holds your images.
Write out:
IMG_DIR = 'C:\';
filename_root = 'image';
IMG_EXT = '.jpg';
NUM_IMAGES = 20;
for i = 1:NUM_IMAGES
imwrite(im{i}, [IMG_DIR filename_root num2str(i) IMG_EXT]);
end
Read in:
for i = 1:NUM_IMAGES
im{i} = imread([IMG_DIR filename_root num2str(i) IMG_EXT]);
end
If you don't know how many there are, you can also use ls command (works differently in Windows vs. Linux).
If you don't know, in advance, which files will be in there, but you know that they have the string in them, 'rawImage' (like 'rawImage001.jpg' etc.) you can do something like
a = dir('c:\temp');
requiredBaseFileName = 'rawImage'; % you want them to contain the substring 'rawImage'
for i = 1:length(a),
fileName = a(i).name;
if(isempty(strfind(fileName,'.jpg')) & isempty(strfind(fileName,'.png')))
continue;
end
if(isempty(strfind(fileName,requiredBaseFileName)))
continue;
end
% do your processing here
end