Prev: Eigen value decomposition
Next: HMM problems
From: Kevin McCarty on 6 Jun 2010 17:02 It would be nice to be able to find a simple S-Function I can use a a template to output some results from Simulink dlmwrite("c:\\temp\\testme.txt", uPtrs, "-append"); OR csvwrite('c:\\temp\\testme.txt', uPtrs); = unresolved external symbol xxxx FILE * fid; fid = fopen("C:\\temp\\testme.txt", 'a'); if(fid != NULL) fclose(fid); = whole application crashes I have not been able to locate a single working example of an S-Function that write to a file. Anyone have any suggestions? TIA
From: Phil Goddard on 6 Jun 2010 23:03 Assuming you are writing a C-mex S-function then, > dlmwrite("c:\\temp\\testme.txt", uPtrs, "-append"); > OR > csvwrite('c:\\temp\\testme.txt', uPtrs); > > = unresolved external symbol xxxx Neither of the above will work because dlmwrite and cvswrite are MATLAB m-code functions, not general C language functions. Hence your S-Function won't compile. > > FILE * fid; > fid = fopen("C:\\temp\\testme.txt", 'a'); > if(fid != NULL) > fclose(fid); > > = whole application crashes The fid line needs double quotes around "a" (i.e. C language syntax) rather than single quotes 'a' (which is m-code syntax). With single quotes I wouldn't expect the code to compile so it's not clear what you mean by the whole application crashes. Apart from the double quotes problem, what you have will work. For a more complete example have a look in the doc under Simulink->User's Guide->Developing S-Functions->Using Work Vectors->Elementary Work Vectors->Elementary Work Vector Examples Phil.
From: Kevin McCarty on 7 Jun 2010 01:22 Yes, I am trying to put together a c-mex function. It does compiles and sends the whole environment into oblivion when I run the simulation. It works however, when I change the quotes like you said. All I want to do is dump the contents of an output to a file but I just can't find any working c-mex code anywhere, or m-files or anything else. I figure a call to fwrite should work, but so far my size parameters are all wrong and I don't know where to go to find out what they should be. I don't understand why something so fundamental should be so hard. But thanks for getting me past the first part. "Phil Goddard" <philNOSPAM(a)goddardconsulting.ca> wrote in message <huhnh7$mtq$1(a)fred.mathworks.com>... > Assuming you are writing a C-mex S-function then, > > > dlmwrite("c:\\temp\\testme.txt", uPtrs, "-append"); > > OR > > csvwrite('c:\\temp\\testme.txt', uPtrs); > > > > = unresolved external symbol xxxx > > Neither of the above will work because dlmwrite and cvswrite are MATLAB m-code functions, not general C language functions. > Hence your S-Function won't compile. > > > > > FILE * fid; > > fid = fopen("C:\\temp\\testme.txt", 'a'); > > if(fid != NULL) > > fclose(fid); > > > > = whole application crashes > > The fid line needs double quotes around "a" (i.e. C language syntax) rather than single quotes 'a' (which is m-code syntax). > With single quotes I wouldn't expect the code to compile so it's not clear what you mean by the whole application crashes. > > Apart from the double quotes problem, what you have will work. > For a more complete example have a look in the doc under > Simulink->User's Guide->Developing S-Functions->Using Work Vectors->Elementary Work Vectors->Elementary Work Vector Examples > > Phil.
From: Phil Goddard on 7 Jun 2010 21:06 If the file doesn't need to be a text file, then why not just use the "To File" block to write to a .mat file. If it does need to be a text file, then it's far easier to just have Simulink write to the MATLAB workspace (using a "To Workspace" block, or through a scope, or through an outport, or any one of various other approaches) and then at the end of the simulation use dlmwrite or csvwrite to write the created MATLAB variable to file. Phil.
From: Kevin McCarty on 7 Jun 2010 22:24
Hi Phil, Unfortunately the requirement is to pull the simulation data for each time slice into a relational database, so we are talking about an ongoing process with a lot of data capture. I still have to get the data to an external database somehow and do it real-time and going through Matlab doesn't sound particularly easy. I would have thought this to be a pretty common requirement so I am scratching my head a bit as to why this isn't an easier task. Every other development tool I have ever used makes this a pretty straightforward process. Kevin "Phil Goddard" <philNOSPAM(a)goddardconsulting.ca> wrote in message <huk51s$bfr$1(a)fred.mathworks.com>... > > If the file doesn't need to be a text file, then why not just use the "To File" block to write to a .mat file. > > If it does need to be a text file, then it's far easier to just have Simulink write to the MATLAB workspace (using a "To Workspace" block, or through a scope, or through an outport, or any one of various other approaches) and then at the end of the simulation use dlmwrite or csvwrite to write the created MATLAB variable to file. > > Phil. |