Store a Vector as csv-File in a Mex File

1 回表示 (過去 30 日間)
Christoph
Christoph 2012 年 8 月 22 日
Hi guys,
I have a small Problem with storing a Vector as CSV-File using a mex function. I my goal is an nice and tidy implementation. For that reason I tried the following code:
mdlStart
FILE *PtrTextID = ssGetPWork(S,3);
PtrTextID = fopen("Storage.txt", "w");
mdlUpdate
FILE *PtrTextID = ssGetPWork(S,3);
fprintf(PtrTextID, "Test");
mdlTerminate
FILE *PtrTextID = ssGetPWork(S,3);
fclose(PtrTextID);
This code leads to a Matlab crash. But if I'm using
mdlUpdate
FILE *PtrTextID
PtrTextID = fopen("Storage.txt", "w");
fprintf(PtrTextID, "Test");
fclose(PtrTextID);
the code works pretty well and do the thinks it's used to. Thats why my guess is that ssGetPWork is not the right workspace to store an FILE-Structure. But what is the right Workspace?
Thanks for your help, CN CN

採用された回答

Kaustubha Govind
Kaustubha Govind 2012 年 8 月 22 日
ssGetPWork takes only one input argument, and returns a void**. Also you need to initialize the number of PWorks first. I haven't tested this out, but I think your code should look like:
mdlInitializeSizes
ssSetNumPWork(S, 1);
mdlStart
ssGetPWork(S)[0] = (void *)fopen("Storage.txt", "w");
mdlUpdate
FILE *PtrTextID = (FILE *)(ssGetPWork(S)[0]);
fprintf(PtrTextID, "Test");
mdlTerminate
FILE *PtrTextID = (FILE *)(ssGetPWork(S)[0]);
fclose(PtrTextID);
Note: ssGetPWork(S)[0] can be replaced with ssGetPWorkValue(S, 0)
  1 件のコメント
Christoph
Christoph 2012 年 8 月 23 日
Hi Kaustubha,
tanks for your excellent help. Your code just works perfect. As you mananged in your note I mixed up getting Values from the DWorkSpace and the PWorkSpace.
kind regards, CN

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by