I am a new Octave user. My Octave version is 4.4.1.
I need help on how to use the parallel package.
I have a function modele_file that takes in as input a class structure that contains the path to files. I have to load that 'mat' files (see the code below).
The problem is that octave indicates that my entry is not defined.
I would be grateful if someone can help me find what is missing in my code
files = dir('./../data/*.mat');
[row col] = size(files);
for k = 1:1000
name{k} = getfield(files, {k,1}, 'name');
end
fun_str = #(stg) strcat("./../data/", stg) ;
vec_name = arrayfun(fun_str, name) ;
vec_result = arrayfun(fun_str, repmat({"result/"}, row,1)) ;
a = [vec_name, vec_result'] ;
struct_info = cell2struct(a, {"name", "result"}, 1);
solution = pararrayfun(nproc, modele_file, struct_info)
Here an example of my function :
function modele = modele_file(info_struct)
file = info_struct{1} ;
path = info_struct{2} ;
load(strcat(file)) ;
modele.X = zeros(2,2) ;
save('file.mat', 'modele') ;
Although my function is able to work. My priority is to launch my function, I do not specifically need it to register in an object.
Thank you.
Related
Is there a possibility to add information like "Version = 1.2.3.4" to - let's say - a TXT file? Could this be achieved with NTFS metadata? If so, can I set such information by a program?
Thanks in advance for any hint! Bernd
You can use either Alternative Data Stream (ADS), or Extended File Attributes (more e.g. here).
If you are sure your files remain on NTFS, Alternative Data Streams are perfect way how to store any amount of alternative data - but you will lose ADS when such file leaves NTFS.
Another option is to use Extended File Attributes, it's supported cross-platform, cross-FS, but it has limitations (e.g. how much data you can store). If you're going to save just e.g. version information, this is probably the best way to go.
Thank you Robert! I used the Extended File Attributes of NTFS that you proposed. They are called "File Summary Information". Unfortunately the information does not show up for - lets say - a text file in the "Details" tab of the Windows file explorer. I found some tricks for the registry but they didn't work yet. My code is Delphi:
CONST FmtID_SummaryInformation:TGUID= '{F29F85E0-4FF9-1068-AB91-08002B27B3D9}';
// FMTID_DocSummaryInformation:TGUID='{D5CDD502-2E9C-101B-9397-08002B2CF9AE}';
// FMTID_UserDefinedProperties:TGUID='{D5CDD505-2E9C-101B-9397-08002B2CF9AE}';
IID_IPropertySetStorage:TGUID= '{0000013A-0000-0000-C000-000000000046}';
STGFMT_FILE=3; //Indicates that the file must not be a compound file.
//This element is only valid when using the StgCreateStorageEx
//or StgOpenStorageEx functions to access the NTFS file system
//implementation of the IPropertySetStorage interface.
//Therefore, these functions return an error if the riid
//parameter does not specify the IPropertySetStorage interface,
//or if the specified file is not located on an NTFS file system volume.
STGFMT_ANY=4; //Indicates that the system will determine the file type and
//use the appropriate structured storage or property set
//implementation.
//This value cannot be used with the StgCreateStorageEx function.
// Summary Information
PID_TITLE = 2;
PID_SUBJECT = 3;
PID_AUTHOR = 4;
PID_KEYWORDS = 5;
PID_COMMENTS = 6;
PID_TEMPLATE = 7;
PID_LASTAUTHOR = 8;
PID_REVNUMBER = 9;
PID_EDITTIME = 10;
PID_LASTPRINTED = 11;
PID_CREATE_DTM = 12;
PID_LASTSAVE_DTM = 13;
PID_PAGECOUNT = 14;
PID_WORDCOUNT = 15;
PID_CHARCOUNT = 16;
PID_THUMBNAIL = 17;
PID_APPNAME = 18;
PID_SECURITY = 19;
(*
// Document Summary Information
PID_CATEGORY = 2;
PID_PRESFORMAT = 3;
PID_BYTECOUNT = 4;
PID_LINECOUNT = 5;
PID_PARCOUNT = 6;
PID_SLIDECOUNT = 7;
PID_NOTECOUNT = 8;
PID_HIDDENCOUNT = 9;
PID_MMCLIPCOUNT = 10;
PID_SCALE = 11;
PID_HEADINGPAIR = 12;
PID_DOCPARTS = 13;
PID_MANAGER = 14;
PID_COMPANY = 15;
PID_LINKSDIRTY = 16;
PID_CHARCOUNT2 = 17;
*)
FUNCTION IsNTFS(AFileName:AnsiString):Boolean;
VAR fso,drv:OleVariant;
BEGIN
fso:=CreateOleObject('Scripting.FileSystemObject'{=});
drv:=fso.GetDrive(fso.GetDriveName(AFileName));
Result:=drv.FileSystem='NTFS'{=};
END;
FUNCTION StgOpenStorageEx(
CONST pwcsName:POleStr; //Pointer to the path of the
//file containing storage object
grfMode:LongInt; //Specifies the access mode for the object
stgfmt:DWORD; //Specifies the storage file format
grfAttrs:DWORD; //Reserved; must be zero
pStgOptions:Pointer;//Address of STGOPTIONS pointer
reserved2:Pointer; //Reserved; must be zero
riid:PGUID; //Specifies the GUID of the interface pointer
OUT stgOpen:IStorage//Address of an interface pointer
) : HResult; stdcall; external 'ole32.dll'{=};
FUNCTION GetFileSummaryInfo(FileName:AnsiString):AnsiString;
{Read the File Summary Info of a file (NTFS)}
FUNCTION PropertyPIDToCaption(CONST ePID:Cardinal):AnsiString;
BEGIN {PropertyPIDToCaption}
CASE ePID OF
PID_TITLE: Result:='Title';
PID_SUBJECT: Result:='Subject';
PID_AUTHOR: Result:='Author';
PID_KEYWORDS: Result:='Keywords';
PID_COMMENTS: Result:='Comments';
PID_TEMPLATE: Result:='Template';
PID_LASTAUTHOR: Result:='Last Saved By';
PID_REVNUMBER: Result:='Revision Number';
PID_EDITTIME: Result:='Total Editing Time';
PID_LASTPRINTED: Result:='Last Printed';
PID_CREATE_DTM: Result:='Create Time/Date';
PID_LASTSAVE_DTM:Result:='Last Saved Time/Date';
PID_PAGECOUNT: Result:='Number of Pages';
PID_WORDCOUNT: Result:='Number of Words';
PID_CHARCOUNT: Result:='Number of Characters';
PID_THUMBNAIL: Result:='Thumbnail';
PID_APPNAME: Result:='Creating Application';
PID_SECURITY: Result:='Security';
ELSE Result:='$'+IntToHex(ePID,8);
END
END; {PropertyPIDToCaption}
VAR i,k:Integer;
PropSetStg:IPropertySetStorage;
PropSpec:ARRAY OF TPropSpec;
PropStg:IPropertyStorage;
PropVariant:ARRAY OF TPropVariant;
Rslt:HResult;
S:AnsiString;
Stg:IStorage;
PropEnum:IEnumSTATPROPSTG;
HR:HResult;
PropStat:STATPROPSTG;
AHRes:HRESULT;
PFNw,P:PWideChar;
BEGIN {GetFileSummaryInfo}
GetMem(P,257); PFNw:=StringToWideChar(FileName,P,256);
Result := '';
TRY
OleCheck(StgOpenStorageEx(PFNw,STGM_READ OR STGM_SHARE_DENY_WRITE,STGFMT_FILE,0,NIL,NIL,#IID_IPropertySetStorage,Stg));
PropSetStg:=Stg AS IPropertySetStorage;
AHRes:=PropSetStg.Open(FmtID_SummaryInformation,STGM_READ OR STGM_SHARE_EXCLUSIVE,PropStg);
IF AHRes<>S_OK THEN Exit;
OleCheck(AHRes);
OleCheck(PropStg.Enum(PropEnum));
hr:=PropEnum.Next(1,PropStat,NIL);
i:=0;
WHILE hr=S_OK DO BEGIN
inc(i);
SetLength(PropSpec,I);
PropSpec[i-1].ulKind:=PRSPEC_PROPID;
PropSpec[i-1].propid:=PropStat.propid;
hr := PropEnum.Next(1,PropStat, nil);
END;
SetLength(PropVariant,i);
Rslt:=PropStg.ReadMultiple(i,#PropSpec[0],#PropVariant[0]);
IF Rslt=S_FALSE THEN Exit;
FOR k:=0 TO i-1 DO BEGIN
S:='';
IF (PropVariant[k].vt=VT_LPWSTR) AND Assigned(PropVariant[k].pwszVal) THEN
S:=WideCharToString(PropVariant[k].pwszVal);
IF (PropVariant[k].vt=VT_LPSTR) AND Assigned(PropVariant[k].pszVal) THEN
S:=PropVariant[k].pszVal;
S:=PropertyPIDToCaption(PropSpec[k].Propid)+'='+S;
IF S<>'' THEN Result:=Result+S+#13#10;
END;
FINALLY
END;
END; {GetFileSummaryInfo}
PROCEDURE SetFileSummaryInfo(FileName,Author,Title,Subject,Keywords,Comments:AnsiString);
{Write some fields of the File Summary Info of a file (NTFS)}
VAR PropSetStg:IPropertySetStorage;
PropSpec:ARRAY OF TPropSpec;
PropStg:IPropertyStorage;
PropVariant:ARRAY OF TPropVariant;
Stg:IStorage;
PFNw,P:PWideChar;
Anz:LongInt;
BEGIN {SetFileSummaryInfo}
IF NOT IsNTFS(FileName) THEN Exit;
Anz:=5;
GetMem(P,257); PFNw:=StringToWideChar(FileName,P,256);
OleCheck(StgOpenStorageEx(PFNw,STGM_SHARE_EXCLUSIVE OR STGM_READWRITE,STGFMT_ANY,0,NIL,NIL,#IID_IPropertySetStorage,Stg));
PropSetStg:=Stg AS IPropertySetStorage;
OleCheck(PropSetStg.Create(FmtID_SummaryInformation,FmtID_SummaryInformation,PROPSETFLAG_DEFAULT,STGM_CREATE OR STGM_READWRITE OR STGM_SHARE_EXCLUSIVE,PropStg));
Setlength(PropSpec,Anz);
PropSpec[0].ulKind:=PRSPEC_PROPID;
PropSpec[0].propid:=PID_AUTHOR;
PropSpec[1].ulKind:=PRSPEC_PROPID;
PropSpec[1].propid:=PID_TITLE;
PropSpec[2].ulKind:=PRSPEC_PROPID;
PropSpec[2].propid:=PID_SUBJECT;
PropSpec[3].ulKind:=PRSPEC_PROPID;
PropSpec[3].propid:=PID_KEYWORDS;
PropSpec[4].ulKind:=PRSPEC_PROPID;
PropSpec[4].propid:=PID_COMMENTS;
SetLength(PropVariant,Anz);
PropVariant[0].vt:=VT_LPSTR;
PropVariant[0].pszVal:=PChar(Author);
PropVariant[1].vt:=VT_LPSTR;
PropVariant[1].pszVal:=PChar(Title);
PropVariant[2].vt:=VT_LPSTR;
PropVariant[2].pszVal:=PChar(Subject);
PropVariant[3].vt:=VT_LPSTR;
PropVariant[3].pszVal:=PChar(Keywords);
PropVariant[4].vt:=VT_LPSTR;
PropVariant[4].pszVal:=PChar(Comments);
OleCheck(PropStg.WriteMultiple(Anz,#PropSpec[0],#PropVariant[0],2));
PropStg.Commit(STGC_DEFAULT);
FreeMem(P);
END; {SetFileSummaryInfo}
I am trying to create a moodle-STACK question with an interactive element via jsxgraph.
Within the [[jsxgraph]] ... [[/jsxgraph]] tags, I am plotting a curve with two databases, each with 6 elements.
Since I would like students to add error bars for both scales, I added a loop within the jsxgraph-element:
var plot = function() {
var j, xr0, yr0, xr1, yr1, xr2, yr2, err1, err2;
board.suspendUpdate();
for (j=0; j<6;j++) {
const v1 = Number(input1.Value());
const v2 = Number(input2.Value());
xr1 = dataX[j]-v1;
yr1 = dataY[j]+v2;
xr2 = dataX[j]+v1;
yr2 = dataY[j]-v2;
xr0 = dataX[j];
yr0 = dataY[j];
err1 = board.create('line',[[xr1,yr0],[xr2,yr0]],{straightFirst:false,straightLast:false, fixed:true});
err2 = board.create('line', [[xr0,yr1],[xr0,yr2]],{straightFirst:false,straightLast:false, fixed:true});
}
};
Since the jsxgraph code is within the HTML-text field of the STACK-question in moodle, the < sign is converted to < and now the loop cant be executed.
Is there a way to use a < sign within the HTML-field of moodle? If not, how can I get past this problem?
I already wrote a running Javascript-Code that shows what I want to create within moodle:
https://jsfiddle.net/Furfinator/3q8yk7hp/15/
I use AppDesigner inMATLAB to show photos with changed RGB. But there is the problem with character of the photo.
When I switch on my own fuction "changeRGB", finally "choosenImage" has 20bytes, class "char" and size(1x10). OK!
There is no problem in using the "function OpenButtonValueChanged". OK!
There is the problem with "function UploadButtonPushed". OK!
ABOUT THE PROBLEM:
When I click button which callback is "function UploadButtonPushed" I get the error:
"Error using imread>parse_inputs (line 502)
The file name or URL argument must be a character
vector or string scalar."
"Error in imread (line 342)
[source, fmt_s, extraArgs, was_cached_fmt_used] =
parse_inputs(cached_fmt, varargin{:});"
WHY?
Because in the "function UploadButtonPushed" my choosenImage has 1977624bytes, class "uint8" and size(681x968x3). So it's too bug for "imread".
WHAT I TRIED:
When in "function OpenButtonValueChanged" I convert a photo, adding "char": (myimage = char(app.clickedImage)); the class of the photo is changing from uint8 to char but the size.
When I use "num2cell", the claas of the photo is changing on "cell" but size and number of bytes are the same- so big. And I get the Error: "Error using imread>parse_inputs (line 502) The file name or URL argument must be a character vector or string scalar."
In my own function "changeRGB" I used "imread(image)" and here is the problem with the size of the photo. Do you know how to get the correct one?
%my own properties in AppDesigner- to use them in different functions
properties (Access = public)
clickedImage;
addR = 1;
addG = 1;
addB = 1;
end
%first function in AppDeesigner
function OpenButtonValueChanged(app, event)
value = app.OpenButton.Value;
[file, howManyFiles] = chooseImagesFromComputer; %myown function
%I load 3 images which are showed as miniatures
myFile1 = imread(file{1});
imshow(myFile1, 'Parent', app.UIAxes1_1);
myFile2 = imread(file{2});
imshow(myFile2, 'Parent', app.UIAxes1_2);
myFile3 = imread(file{3});
imshow(myFile3, 'Parent', app.UIAxes1_3);
%take values of changed RGB from the slider
app.addR = app.SliderR.Value
app.addG = app.SliderG.Value
app.addB = app.SliderB.Value
%work only on one image to change its colors. app.clickedImage, app.addR, app.addG, app.addB are properties at the beginning of the code
app.clickedImage = file{1};
app.clickedImage = changeRGB(app.clickedImage,app.addR,app.addG,app.addB); %changeRGB- my own function- here is the problem. I add it bottom
imshow(app.clickedImage,'Parent',app.UIAxesMain);
end
%second function in AppDesigner
%here is the button to upload color of the photo
function UploadButtonPushed(app, event)
myimage = app.clickedImage;
myimage = changeRGB(myimage,app.addR,app.addG,app.addB);
imshow(myimage);
end
%here is my own function in matlab, not in AppDesigner, which makes problem:
function [changedImage] = changeRGB(choosenImage, addR, addG, addB)
whos
loadedImage = imread(choosenImage);
R = loadedImage(:,:,1); %extract one of the color channels
G = loadedImage(:,:,2);
B = loadedImage(:,:,3);
RBG = cat(3,R,G,B);
R_adj2 = R + addR;
G_adj2 = G + addG;
B_adj2 = B + addB;
changedImage = cat(3,R_adj2,G_adj2,B_adj2);
end
First, you make unnecessary operations in changeRGB
function [changedImage] = changeRGB(choosenImage, addR, addG, addB)
loadedImage = imread(choosenImage);
loadedImage = bsxfun(#sum, loadedImage, reshape([addR, addG, addB], [1 1 3]);
end
Then this function return an array (the modified image) so in UploadButtonPushed(app, event) when you run myimage = app.clickedImage;, you are passing the modified array instead of the image path, that you set here app.clickedImage = changeRGB(app.clickedImage,app.addR,app.addG,app.addB);
So you have to change the design of your variables, because app.clickedImage is saving either the image path, or the image itself. Consider having 2 different variables.
A good advice also is to use matlab debugger which is really good help to find the source of this kind of problems.
Is there a way to create a Simulink bus from the definition of a C struct? Let's say I have some C struct definition in a header file:
typedef struct {
double a, b;
} u_T;
Can I use this to automatically generate a Simulink.Bus object?
Edit: Is there a tool that generates Matlab code for creating Simulink.Bus objects describing the structs from a .h file?
This is supported in the latest version of MATLAB (2017a). Use the following command.
importInfo = Simulink.importExternalCTypes(headerFiles)
For more info see: https://www.mathworks.com/help/simulink/slref/simulink.importexternalctypes.html
You can import a header when creating a bus object, but this is only used for code generation with Simulink Coder, not for normal simulation with Simulink. See the documentation on Simulink.Bus for more details.
The only way to do what you want would be to write a parser that reads your .h file and creates a bus object in the MATLAB workspace. I don't know of any such tool I'm afraid.
Here is a MATLAB script:
filename = 'Test3.h';
fid = fopen(filename);
tline = fgetl(fid);
i = 1;
while ischar(tline)
% Search for structs
if any(strfind(tline,'typedef struct'))
tline = fgetl(fid);
% Get elements
while ~any(strfind(tline,'}'))
% Create Element
el(i) = Simulink.BusElement;
c = regexp(tline,'(\w*) (\w*);','tokens');
% Element Name
el(i).Name = c{1,1}{1,2};
% Element Data type
switch c{1,1}{1,1}
case 'float'
el(i).DataType = 'single';
case 'int8_t'
el(i).DataType = 'int8';
case 'uint32_t'
el(i).DataType = 'uint32';
% Add unknown Data types as cases here
otherwise
el(i).DataType = ['Bus: ',c{1,1}{1,1}];
end
i = i+1;
tline = fgetl(fid);
end
% Get struct name
c = regexp(tline,'} (\w*);', 'tokens');
% Create bus
eval([c{1,1}{1,1},' = Simulink.Bus;']);
% Assign elements
eval([c{1,1}{1,1},'.Elements = el;']);
% Assign Header file
eval([c{1,1}{1,1},'.HeaderFile = ''',filename,''';']);
clear el; i = 1;
end
tline = fgetl(fid);
end
fid = fclose(fid);
clear c fid filename i tline;
I hope it helps...
I know this is a simple question, but for some reason I can't find a straight answer that works no matter where I look.
Basically, I have 4 values that were found in one m file, and I want to run them through a separate m file and retrieve the output from it.
I tried something like these, but none worked:
result = generate(nrow,ncol,a,b);
function result = generate(nrow,ncol,a,b);
result = #generate(nrow,ncol,a,b);
The final value in the m file "generate" is called result, and I'm trying to carry that across to my initial m file.
Any advice as to what I'm doing wrong would be greatly appreciated! Please and thank you
if your file generate.m defines a function it should have itself the following structure (which takes into account the fact that you have four returned values)
function [ret1 ret2 ret3 ret4] = generate(nrow,ncol,a,b)
.... % # Some processing of yours
ret1 = ... ; % # Returned values are eventually set
ret2 = ... ;
ret3 = ... ;
ret4 = ... ;
end
The function should be called (e.g. in your main script) as
[ret1 ret2 ret3 ret4] = generate(nrow,ncol,a,b);
now you have the variables ret1,ret2,ret3,ret4 available in the caller scope.
Be aware that the file generate.m must be in the current matlab PATH.