Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

XPC Target Embedded Filescope Dynamic File Naming

Asked by James Robertson on 27 Apr 2012

I’m having trouble with the dynamic file naming of Filescope Blocks in xPC Embedded Option. I would like the file name of a scope to increment each time the software boots so I can record data from multiple test runs.

I have added '_<%%%>' to the file name (now ‘ins_<%%%>’) and checked ‘auto restart’ and ‘dynamic file naming’ (not that I want to limit the size of each file with ‘auto restart’ but it is the only way to get the ‘dynamic file naming’ box to appear). If I boot the software 3 times I get one file named ins_001.dat with the correct data in it, one file named ins_002.dat that is empty and only 1KB in size, file ins_003.dat is missing.

I have also tried running the software without either 'auto restart' or 'dynamic file namining' checked but that doesn't work.

Any ideas?

1 Comment

Chris Jones on 16 Aug 2012

Unfortunately, I do not have a solution, only emphasis on the issue set forth by James above.

I am having a similar problem, only in my 2009a version, the initial file D_<%%%>.dat isn't even showing up. When I eliminate the '<%%%>', the first file saves with no problem. I do not have a 'dynamic file naming' option in 2009a, but I do not receive an error, the program builds, loads and executes.

Thoughts?

James  Robertson

1 Answer

Answer by Kent Schonert on 29 Nov 2012
Accepted answer

The dynamic file naming enables additional files to be created when the max file size is reached for a given file. Autorestart is not for logging across power cycles.

Each time the xPC (FreeDOS) starts the application file, new (mostly empty) data files for the scopes are created, replacing the ones from the previous power cycle.

What I did: To address the issue of preserving data between reboots, I modified the autoexec.bat file to backup my data files from the previous power cycle, before it loads the stand alone application. Due to FreeDOS command set limitations, I compiled an .exe that renames the files to include the current day & time. The autoexec calls my .exe to rename the files and then moves it to a data folder. I used the OpenWatcom compiler/IDE to make a compatible 32-bit DOS executable.

Keep in mind that the filenames are limited to 8 characters, not including the extension.

3 Comments

James Robertson on 30 Nov 2012

Kent,

That sounds like a good work around which could save me a lot of time. Would you mind sharing your code with those of us less able to program?

Does your .exe preserve any element of the original file name? The reason I ask is that I log several scopes at once, so generate 5 .dat files simultaneously (ins.dat, ain.dat, din.dat, val.dat and cnt.dat), I would need them to end up with differing file names even though they were all created at the same time.

Many Thanks, James

Kent Schonert on 6 Dec 2012

James,

I was logging several file at once as well. My code is below. It is called several times from the autoexec.bat (you can also call from runxpc.bat).

in autoexe.bat I did:

dir > dir1.txt
moved.exe curr.dat c_ .dat
moved.exe inp.dat i_ .dat
moved.exe out.dat o_ .dat
moved.exe batt.dat b_ .dat
dir > dir2.txt
move /Y c_*.dat c:\data
move /Y i_*.dat c:\data
move /Y o_*.dat c:\data
move /Y b_*.dat c:\data
dir data > dir3.txt

The dir commands were put in troubleshooting, and stayed there because the pauses stopped weird behavior.

My c program compiled in OpenWatcom:

//usage: moved.exe orignal_filename.ext new_filename new_filename_extension
//example:      moved.exe curr_.dat c_ .dat   
//                      will rename curr_.dat to c_241040.dat on 24th at 10:40 PM
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <ctime>
#include <stdlib.h>
#include <sstream>
#include <string>
using namespace std;
static unsigned get_file_size (const char * file_name)
{
    struct stat sb;
    if (stat (file_name, & sb) != 0) {
        return 0;
    }
    return sb.st_size;
}
int main(int argc, char* argv[]){
    char origFName[12];
    char fPrefix[8],extension[8], temp[8];
    char newFname[16];
      strcpy(origFName,argv[1]);
      strcpy(fPrefix,argv[2]);
      strcpy(extension,argv[3]);
      //bail if this file is basically empty (or if it doesn't exist)
      if( get_file_size(origFName) < 1000)
          return 0;
      time_t t = time(0);   // get time now
      struct tm * now = localtime( & t );
      //rename file name to current "x_MMDDmm.dat"
      strcpy(newFname, fPrefix);        
      strcat(newFname, itoa(now->tm_mday,temp,10));
      strcat(newFname, itoa(now->tm_hour,temp,10));
      strcat(newFname, itoa(now->tm_min,temp,10));
      strcat(newFname, extension);
      cout<<"renaming to "<<newFname<<endl;
      rename ( origFName, newFname);
      return 0;
}

(BTW, OpenWatcom doesn't fully support STL.)

Sorry for not getting back to you sooner. I need to see if I can have the site email me when I get a reply.

Cheers,

-Kent

James Robertson on 14 Dec 2012

Kent,

Thanks for posting the code. I had some trouble getting it running, reading up on it, it appears that my version of DOS is very basic an has only the internal commands; for example it can't handle the MOVE command.

To overcome the problem I wrote my own script (which took ages as I was clueless) using only the most basic DOS commands available. It is a bit crude in that it will only go up to 30 runs, but does that job for me. I had far flashier scripts running in cmd.exe and command.exe (for example, they would increment the file name using 'variable=%variable%+1', or name the file with its creation date), but found that they would produce 'bad command or file name' errors on my embedded computer.

If anyone wants to use my script, just cut and paste it into note pad and save as xpcfile.bat. The autoexec.bat will need a tweak and off you go.

if true
  % code
::File Name - xpcfile
::Description - A script for moving .dat files to a folder with labled with the run number (max 30 runs). This script must run before xpc boots or else .dats will be over-written.
::Purpose - To overcome a glitch in MatLab xPC Target Embedded that means data is over-written each run.
::Author - James Robertson.
::Disclaimer - I don't really know anything about programming and have cobbled this script together form stuff I have read on Google. Whilst it works for me, I have not tested it exhaustivley and therefore it may blow your computer up. I won't be held responsible if it does. 
::How to Use - 1) Modify the 4th to last line of this script to call your specific xPC .rtb file (note max 8 characters). 2) Add a line to call this script from autoexec.bat file and remove the line calling the xPC .rtb (this script should do it).   
::-------------------------------------------------------------------------------
:: Dont Print Steps to Screen.
::-------------------------------------------------------------------------------
@ECHO OFF
:: The Main Script - Checks for a folder called run1 and, if it doesn't exist, creates it and moves .dat files into it. If run1 does exist the process is repeated for run2 etc..........
::-------------------------------------------------------------------------------
::Look for .dat files, if found run ":RUNS", if none found skip to ":DONE".
IF EXIST *.dat GOTO :RUN1
IF NOT EXIST *.dat GOTO :DONE
::The run number increment bit. Note a max of 30 runs can be saved, this could be expanded if you need to by cutting and pasting the code and changing the numbers.
::--------------------------------------------------------------------------------
:RUN1
SET RUNNO=1
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN2
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN2
SET RUNNO=2
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN3
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN3
SET RUNNO=3
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN4
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN4
SET RUNNO=4
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN5
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN5
SET RUNNO=5
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN6
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN6
SET RUNNO=6
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN7
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN7
SET RUNNO=7
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN8
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN8
SET RUNNO=8
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN9
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN9
SET RUNNO=9
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN10
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN10
SET RUNNO=10
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN11
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN11
SET RUNNO=11
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN12
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN12
SET RUNNO=12
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN13
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN13
SET RUNNO=13
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN14
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN14
SET RUNNO=14
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN15
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN15
SET RUNNO=15
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN16
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN16
SET RUNNO=16
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN17
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN17
SET RUNNO=17
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN18
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN18
SET RUNNO=18
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN19
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN19
SET RUNNO=19
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN20
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN20
SET RUNNO=20
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN21
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN21
SET RUNNO=21
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN22
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN22
SET RUNNO=22
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN23
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN23
SET RUNNO=23
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN24
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN24
SET RUNNO=24
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN25
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN25
SET RUNNO=25
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN26
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN26
SET RUNNO=26
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN27
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN27
SET RUNNO=27
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN28
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN28
SET RUNNO=28
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN29
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN29
SET RUNNO=29
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :RUN30
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
:RUN30
SET RUNNO=30
ECHO run%RUNNO%
IF EXIST \run%RUNNO%\*.dat GOTO :ERROR
IF NOT EXIST \run%RUNNO%\*.dat GOTO :FOLDER
::Create folder and copy .dat files to it.
::---------------------------------------------------------------------------------
:FOLDER
ECHO make folder run%RUNNO%
mkdir \run%RUNNO%
COPY *.dat \run%RUNNO%
::Check .dat files have been copied correctly before deleting originals. 
IF EXIST \run%RUNNO%\*.dat GOTO :DELETE
IF NOT EXIST \run%RUNNO%\*.dat GOTO :ERROR
::Delete originals.
----------------------------------------------------------------------------------- 
:DELETE
DEL *.dat
::Provide a point to jump to when done, display message
::---------------------------------------------------------------------------------
:DONE
ECHO Moving of files complete or no .dat files to move. 
::Boot xPC Files when done
::---------------------------------------------------------------------------------
xpcboot Clever~1.rtb
GOTO :EXIT
::Provide a point to jump to if there is an error. EXIT without booting xPC .rtb if files do not copy so they are not overwritten
::---------------------------------------------------------------------------------
:ERROR
ECHO THERE WAS A PROBLEM! Files did not copy to new folder. xPC .rtb Will not launch. You may have exceeded the maximum number of runs - Remove previous runs and try again. 
:EXIT
end

Hope the above helps others; oh and thanks for the idea Kent.

James

Kent Schonert

Contact us