gpt4 book ai didi

c# - 函数在 MATLAB 中正常工作,但在从 .NET 中调用时无法正常工作

转载 作者:行者123 更新时间:2023-11-30 18:03:09 27 4
gpt4 key购买 nike

我正在使用 MATLAB Builder NE 实现互操作性,从作为开源应用程序插件构建的 C# .NET 程序调用 MATLAB 函数 ClearCanvas .当我从 .NET 程序正常运行代码时,我通常(但不总是)收到错误消息

MWMCR::EvaluateFunction error ... Reference to non-existent element of a cell array. Error in => ComputeT1Maps.m at line 178.

有问题的MATLAB代码行如下:

seriesHeader = currentSlab.Slice{1}.MetaData{1}.Header;

Header 是 MATLAB 的 dicominfo 函数给出的形式的结构,MetaData{n} 是包含第 n 个图像文件的文件名和图像头结构的结构。

ComputeT1Maps 函数的函数签名是:

function ComputeT1Maps(data, options)

为了找出这个错误,我将以下行放在 ComputeT1Maps 函数的开头以保留状态,这样我就可以看到从 .NET 传递给 MATLAB 的值:

save(fullfile('F:\MATLAB\T1Mapping', 'T1_debug.mat'), 'data', 'options', ...
'-mat', '-v7.3');

因此,在保留此函数的输入(从调用它的 .NET 程序接收)后,我在加载保存的变量后尝试从交互式 MATLAB session 运行我的 ComputeT1Maps 函数,以便我可以利用 MATLAB 的调试工具来弄清楚为什么我会收到错误。那时候事情变得非常奇怪。当给出与从我的 .NET 程序中调用它时提供给它的完全相同的操作数时,该函数在交互式 MATLAB session 中工作得很好。怎么会这样?从 C# .NET 调用该函数时如何失败,但在交互式 MATLAB session 中给出完全相同的输入时如何正确运行?此外,同样的代码以前可以工作,但只有在我将本地安装的 MATLAB 和 MCR 都更新到最新版本 (2011b) 后才开始出现错误。

在 .NET 端,传递给 MATLAB 的 data 由以下函数构造:

    public void ExchangeData(MultidimensionalDataCollection mdc, string outputPath, bool generateAncillaryTestImages, 
bool excludeAcquisitions, double[] exclusionList, bool showProgressBar, bool displayComputedImages,
bool pauseAtEachSlice, bool softwareDiagnostics, bool displayProgressBar)
{
try
{
int subspaceIndex = 0;
int slabIndex = 0;
int sliceIndex = 0;
int imageIndex = 0;

MWStructArray topLevelGrouping;
MWCellArray geometricSubspaceList;
MWStructArray geometricGrouping;
MWCellArray slabList;
MWStructArray slabGrouping;
MWCellArray sliceList;
MWStructArray sliceGrouping;
MWCellArray imageMetaData;
MWStructArray perImageData;
MWArray[] result;
MWLogicalArray successFlag;
MWStructArray header;
MWCellArray sopInstanceUids;
MWStructArray t1MapOptions;

topLevelGrouping = new MWStructArray(1, 1, new string[] { "GeometricSubspace",
"GeometricSubspaceCount" });
topLevelGrouping["GeometricSubspaceCount", 1] = mdc.Count;
geometricSubspaceList = new MWCellArray(1, mdc.Count);

subspaceIndex = 0;
foreach (GeometricSubspace subspace in mdc)
{
subspaceIndex++;

geometricGrouping = new MWStructArray(1, 1, new string[] { "Slab",
"SlabCount" });
geometricGrouping["SlabCount", 1] = subspace.Count;
slabList = new MWCellArray(1, subspace.Count);

slabIndex = 0;
foreach (Slab slab in subspace)
{
slabIndex++;

slabGrouping = new MWStructArray(1, 1, new string[] { "Slice",
"SliceCount" });
slabGrouping["SliceCount", 1] = slab.Count;
sliceList = new MWCellArray(1, slab.Count);

sliceIndex = 0;
foreach (Slice slice in slab)
{
sliceIndex++;

sliceGrouping = new MWStructArray(1, 1, new string[] {
"ImageCount", "MetaData", "MultidimensionalPixelData",
"SliceLocation", "SopInstanceUids" });
sliceGrouping["ImageCount", 1] = slice.Count;
imageMetaData = new MWCellArray(1, slice.Count);

int rows, columns;
short[,,,] multidimensionalPixelData = null;

imageIndex = 0;
foreach (Image image in slice)
{
imageIndex++;
short[,] imageMatrix = null;

if (!image.ImageSopClass.DicomUid.Equals(DicomUids.MRImageStorage))
throw new NotSupportedException("SopClass " + image.ImageSopClass.Name + " is not supported.");
else
{
DicomUncompressedPixelData rawPixelData = image.PixelData;

imageMatrix = GetCCImageMatrix(rawPixelData, out rows, out columns);

if (imageIndex == 1)
{
multidimensionalPixelData = new short[slice.Count, 1, columns, rows];
}

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
// Remember that C# array indices start with 0 while in MATLAB they start with 1
multidimensionalPixelData[imageIndex - 1, 0, i, j] = imageMatrix[i, j];
}
}
}
perImageData = new MWStructArray(1, 1, new string[] { "FileName", "Header" });
perImageData["FileName", 1] = image.FileName;

result = _mlT1Mapping.QT1GetDicomHeader(2, image.FileName);

if (result == null)
throw new Exception("GetDicomHeader failed to read the header file for filename: " +
image.FileName);
else
{
// MWStructArray
successFlag = (MWLogicalArray)result[0];
bool[] headerObtained = successFlag.ToVector();
if (headerObtained[0])
{
header = (MWStructArray)result[1];
perImageData["Header", 1] = header;

imageMetaData[1, imageIndex] = perImageData;
}
else
{
Console.WriteLine("GetDicomHeader failed to read the header file for filename: " +
image.FileName);
}
}
}
sliceList[1, sliceIndex] = sliceGrouping;
sliceGrouping["MetaData", 1] = imageMetaData;
sliceGrouping["SliceLocation", 1] = slice.SliceLocation;

List<string> theSops = slice._sopList;
sopInstanceUids = new MWCellArray(1, slice._sopList.Count);

int count = 0;
foreach (string sop in theSops)
{
count++;
sopInstanceUids[1, count] = sop;
}
sliceGrouping["SopInstanceUids", 1] = sopInstanceUids;
sliceGrouping["MultidimensionalPixelData", 1] = (MWNumericArray)multidimensionalPixelData;
}
slabList[1, slabIndex] = slabGrouping;
slabGrouping["Slice", 1] = sliceList;
}
geometricSubspaceList[1, subspaceIndex] = geometricGrouping;
geometricGrouping["Slab", 1] = slabList;
}
topLevelGrouping["GeometricSubspace", 1] = geometricSubspaceList;

t1MapOptions = new MWStructArray(1, 1, new string[] { "DirectoryPath",
"ComputeDifferenceImages", "ComputeMultiplicationImages", "DisplayComputedImages", "PauseAtEachSlice",
"SoftwareDiagnostics", "DisplayProgressBar"
});

t1MapOptions["DirectoryPath"] = (MWCharArray)outputPath;
t1MapOptions["SaveS0Maps"] = (MWLogicalArray)generateAncillaryTestImages;
t1MapOptions["ExcludeAcquisitions"] = (MWLogicalArray)excludeAcquisitions;
t1MapOptions["ExclusionList"] = (MWNumericArray)exclusionList;
t1MapOptions["DisplayComputedImages"] = (MWLogicalArray)displayComputedImages;
t1MapOptions["PauseAtEachSlice"] = (MWLogicalArray)pauseAtEachSlice;
t1MapOptions["SoftwareDiagnostics"] = (MWLogicalArray)softwareDiagnostics;
t1MapOptions["DisplayProgressBar"] = (MWLogicalArray)displayProgressBar;

_mlT1Mapping.ComputeT1Maps(topLevelGrouping, t1MapOptions);
}
catch (Exception)
{
throw;
}
}

最佳答案

我还不能 100% 确定我已经修复了所有问题,但到目前为止,经过一些更改后测试似乎取得了成功。问题的症结似乎是一些作业的顺序被调换了。这发生在几个地方。例如,而不是:

sliceList[1, sliceIndex] = sliceGrouping;
sliceGrouping["MetaData", 1] = imageMetaData;

它应该被订购为:

sliceGrouping["MetaData", 1] = imageMetaData;
sliceList[1, sliceIndex] = sliceGrouping;

这个错误的奇怪之处在于代码在以前版本的 MATLAB 中运行得很好。它根本不应该起作用!

关于c# - 函数在 MATLAB 中正常工作,但在从 .NET 中调用时无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7491836/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com