使用matfile()将数据附加到已保存的变量

时间:2017-09-26 07:39:43

标签: matlab append mat-file

我有一些随机的二维数据fuffalo我保存到文件fuffa=rand(10,10); save('fuffalo','fuffa', '-v7.3')

fuffa

然后我经历一个循环,生成其他m1 = matfile('fuffalo.mat', 'Writable', true); for ii=1:3 fuffa2=rand(10,10); m1.fuffa(1:10,1:10,end+1)=fuffa2; end 数据,我想要(在第三维中)附加到已保存的变量。为此,我尝试应用this建议:

ii=2

但是,在 > <?xml version="1.0" encoding="UTF-8"?> <!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0 --> <!-- 2017-09-26T12:38:36 --> <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="scriptlet" pageWidth="1089" pageHeight="842" columnWidth="900" leftMargin="30" rightMargin="30" topMargin="30" bottomMargin="30" isFloatColumnFooter="true" whenResourceMissingType="Empty" uuid="fc422c67-db33-4432-b0fe-9c5b6cbdc01c"> <property name="com.jasperassistant.designer.Grid" value="false"/> <property name="com.jasperassistant.designer.SnapToGrid" value="false"/> <property name="com.jasperassistant.designer.GridWidth" value="10"/> <property name="com.jasperassistant.designer.GridHeight" value="10"/> <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/> <property name="com.jaspersoft.studio.unit." value="pixel"/> <property name="net.sf.jasperreports.print.create.bookmarks" value="false"/> <property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/> <property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/> <property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/> <property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/> <property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/> <property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/> <property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/> <property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/> <parameter name="reportName" class="java.lang.String"/> <parameter name="runDate" class="java.util.Date"/> <parameter name="month" class="java.lang.String"/> <parameter name="year" class="java.lang.Integer"/> <parameter name="stations" class="java.lang.String"/> <parameter name="contractType" class="java.lang.String"/> <parameter name="cash" class="java.lang.String"/> <parameter name="subReportParameter" class="java.lang.String"/> <parameter name="innerSubReportParameter" class="java.lang.String"/> <queryString> <![CDATA[]]> </queryString> <field name="pageHeader" class="java.lang.String"> <fieldDescription><![CDATA[pageHeader]]></fieldDescription> </field> <field name="pageHeaderTotal" class="java.lang.String"> <fieldDescription><![CDATA[pageHeader]]></fieldDescription> </field> <field name="monthlyAmortizationReport" class="java.util.List"> <fieldDescription><![CDATA[monthlyAmortizationReport]]></fieldDescription> </field> <field name="monthlyAmortizationReportTotal" class="java.util.List"> <fieldDescription><![CDATA[monthlyAmortizationReportTotal]]></fieldDescription> </field> <group name="Group1"> <groupHeader> <band height="605"> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> <frame borderSplitType="DrawBorders"> <reportElement stretchType="RelativeToTallestObject" x="0" y="2" width="1030" height="70" uuid="0e09fd68-d674-4ffe-8026-27560a2f6fb8"/> <box> <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> </box> <staticText> <reportElement x="35" y="21" width="100" height="15" uuid="091d6e12-3c88-4711-9278-c2990a483faa"> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> </reportElement> <box leftPadding="5"/> <textElement textAlignment="Left" verticalAlignment="Middle"> <font fontName="SansSerif" isBold="false"/> </textElement> <text><![CDATA[Station(s) Selected:]]></text> </staticText> <textField isStretchWithOverflow="true" pattern="MM/dd/yyyy" isBlankWhenNull="true"> <reportElement x="135" y="21" width="210" height="15" uuid="6e396536-4be3-4b2b-b938-14088da8e46e"/> <box leftPadding="5"/> <textElement textAlignment="Left" verticalAlignment="Middle"> <font fontName="SansSerif" isBold="false"/> </textElement> <textFieldExpression><![CDATA[$P{stations}]]></textFieldExpression> </textField> </frame> <staticText> <reportElement positionType="Float" x="47" y="123" width="240" height="15" uuid="8007ea73-d590-42b2-a7e3-e12e0958fd92"> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> </reportElement> <box leftPadding="5"/> <textElement textAlignment="Left" verticalAlignment="Middle"> <font fontName="SansSerif" isBold="false"/> </textElement> <text><![CDATA[Prepared By: ____________________________]]></text> </staticText> <staticText> <reportElement positionType="Float" x="47" y="178" width="240" height="15" uuid="9d1717cd-fe4d-4ca3-87b0-ba035b59c801"> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> </reportElement> <box leftPadding="5"/> <textElement textAlignment="Left" verticalAlignment="Middle"> <font fontName="SansSerif" isBold="false"/> </textElement> <text><![CDATA[Reviewed By: ____________________________]]></text> </staticText> <staticText> <reportElement positionType="Float" x="690" y="123" width="180" height="15" uuid="0d0d7a65-73ef-48b5-b66d-0bf87d3f6f3a"> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> </reportElement> <box leftPadding="5"/> <textElement textAlignment="Left" verticalAlignment="Middle"> <font fontName="SansSerif" isBold="false"/> </textElement> <text><![CDATA[Date: ________________________]]></text> </staticText> <staticText> <reportElement positionType="Float" x="690" y="178" width="180" height="15" uuid="dfbfed6d-0058-474b-be0a-b0f35998e4bb"> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> <property name="com.jaspersoft.studio.unit.x" value="pixel"/> </reportElement> <box leftPadding="5"/> <textElement textAlignment="Left" verticalAlignment="Middle"> <font fontName="SansSerif" isBold="false"/> </textElement> <text><![CDATA[Date: ________________________]]></text> </staticText> <break> <reportElement positionType="FixRelativeToBottom" x="0" y="600" width="550" height="1" isPrintWhenDetailOverflows="true" uuid="cab606e6-99cd-454a-8749-2d5461d4f9a1"/> </break> </band> </groupHeader> </group> <group name="pageHeader" isStartNewPage="true"> <groupExpression><![CDATA[$F{pageHeader}]]></groupExpression> <groupHeader> <band height="1"> <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.FreeLayout"/> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> <subreport> <reportElement x="0" y="0" width="1029" height="1" isRemoveLineWhenBlank="true" backcolor="#99CCFF" uuid="ec8271ee-6fb9-4c88-b730-b8031517400f"> <property name="com.jaspersoft.studio.unit.width" value="pixel"/> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> </reportElement> <subreportParameter name="pageHeader"> <subreportParameterExpression><![CDATA[$F{pageHeader}]]></subreportParameterExpression> </subreportParameter> <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource ($F{monthlyAmortizationReport})]]></dataSourceExpression> <subreportExpression><![CDATA[$P{subReportParameter}]]></subreportExpression> </subreport> </band> <band height="1"> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> <subreport> <reportElement positionType="Float" x="0" y="0" width="1029" height="1" isRemoveLineWhenBlank="true" backcolor="#99CCFF" uuid="ec8271ee-6fb9-4c88-b730-b8031517400f"> <property name="com.jaspersoft.studio.unit.width" value="pixel"/> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> </reportElement> <subreportParameter name="pageHeader"> <subreportParameterExpression><![CDATA[$F{pageHeader}]]></subreportParameterExpression> </subreportParameter> <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource ($F{monthlyAmortizationReportTotal})]]></dataSourceExpression> <subreportExpression><![CDATA[$P{innerSubReportParameter}]]></subreportExpression> </subreport> </band> </groupHeader> </group> <title> <band height="162" splitType="Stretch"> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> <staticText> <reportElement x="35" y="0" width="70" height="15" uuid="d85beb8a-5178-43a7-8b24-5608250f088c"> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> </reportElement> <box leftPadding="5"/> <textElement textAlignment="Left" verticalAlignment="Middle"> <font fontName="SansSerif" isBold="false"/> </textElement> <text><![CDATA[Report Name:]]></text> </staticText> <textField isBlankWhenNull="true"> <reportElement x="105" y="0" width="552" height="15" uuid="3e8eb96b-1b5e-4f34-b1ec-f5770049d23e"> <property name="com.jaspersoft.studio.unit.width" value="pixel"/> </reportElement> <box leftPadding="5"/> <textElement textAlignment="Left" verticalAlignment="Middle"> <font fontName="SansSerif" isBold="false" isUnderline="false"/> </textElement> <textFieldExpression><![CDATA[$P{reportName}]]></textFieldExpression> </textField> <staticText> <reportElement x="690" y="0" width="60" height="15" uuid="d85beb8a-5178-43a7-8b24-5608250f088c"> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> <property name="com.jaspersoft.studio.unit.y" value="pixel"/> <property name="com.jaspersoft.studio.unit.x" value="pixel"/> </reportElement> <box leftPadding="5"/> <textElement textAlignment="Left" verticalAlignment="Middle"> <font fontName="SansSerif" isBold="false"/> </textElement> <text><![CDATA[Run Date:]]></text> </staticText> <textField pattern="MM/dd/yyyy" isBlankWhenNull="true"> <reportElement x="750" y="0" width="170" height="15" uuid="3e8eb96b-1b5e-4f34-b1ec-f5770049d23e"/> <box leftPadding="5"/> <textElement textAlignment="Left" verticalAlignment="Middle"> <font fontName="SansSerif" isBold="false"/> </textElement> <textFieldExpression><![CDATA[$P{runDate}]]></textFieldExpression> </textField> <frame> <reportElement x="0" y="50" width="1029" height="70" uuid="6df8b9cc-b364-433c-b589-cacf4f0a679b"> <property name="com.jaspersoft.studio.unit.width" value="pixel"/> </reportElement> <box> <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> </box> <textField isBlankWhenNull="true"> <reportElement x="80" y="15" width="200" height="15" uuid="3e8eb96b-1b5e-4f34-b1ec-f5770049d23e"> <property name="com.jaspersoft.studio.unit.x" value="pixel"/> </reportElement> <box leftPadding="5"/> <textElement textAlignment="Left" verticalAlignment="Middle"> <font fontName="SansSerif" isBold="false"/> </textElement> <textFieldExpression><![CDATA[$P{month}]]></textFieldExpression> </textField> <staticText> <reportElement x="35" y="45" width="45" height="15" uuid="d85beb8a-5178-43a7-8b24-5608250f088c"> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> <property name="com.jaspersoft.studio.unit.width" value="pixel"/> </reportElement> <box leftPadding="5"/> <textElement textAlignment="Left" verticalAlignment="Middle"> <font fontName="SansSerif" isBold="false"/> </textElement> <text><![CDATA[Year: ]]></text> </staticText> <textField isBlankWhenNull="true"> <reportElement x="80" y="45" width="200" height="15" uuid="3e8eb96b-1b5e-4f34-b1ec-f5770049d23e"> <property name="com.jaspersoft.studio.unit.y" value="pixel"/> </reportElement> <box leftPadding="5"/> <textElement textAlignment="Left" verticalAlignment="Middle"> <font fontName="SansSerif" isBold="false"/> </textElement> <textFieldExpression><![CDATA[$P{year}]]></textFieldExpression> </textField> <staticText> <reportElement x="35" y="15" width="45" height="15" uuid="617febe3-cdac-4613-8986-b9dbd31a072d"> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> <property name="com.jaspersoft.studio.unit.x" value="pixel"/> <property name="com.jaspersoft.studio.unit.width" value="pixel"/> </reportElement> <box leftPadding="5"/> <textElement textAlignment="Left" verticalAlignment="Middle"> <font fontName="SansSerif" isBold="false"/> </textElement> <text><![CDATA[Month:]]></text> </staticText> </frame> </band> </title> <pageFooter> <band height="15"> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> <textField isStretchWithOverflow="true"> <reportElement x="0" y="0" width="385" height="15" uuid="3e8eb96b-1b5e-4f34-b1ec-f5770049d23e"> <property name="com.jaspersoft.studio.unit.y" value="pixel"/> </reportElement> <box leftPadding="5"/> <textElement textAlignment="Left" verticalAlignment="Middle"> <font fontName="SansSerif" isBold="false" isUnderline="false"/> </textElement> <textFieldExpression><![CDATA[$P{reportName}]]></textFieldExpression> </textField> <textField evaluationTime="Page"> <reportElement positionType="Float" x="386" y="0" width="150" height="15" uuid="e727b263-ae6c-4742-b102-64f0bc117b31"> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> <property name="com.jaspersoft.studio.unit.width" value="pixel"/> </reportElement> <textElement textAlignment="Right" verticalAlignment="Middle"/> <textFieldExpression><![CDATA["Page " + $V{PAGE_NUMBER} + " o"]]></textFieldExpression> </textField> <textField evaluationTime="Report"> <reportElement positionType="Float" x="536" y="0" width="171" height="15" uuid="e727b263-ae6c-4742-b102-64f0bc117b31"> <property name="com.jaspersoft.studio.unit.height" value="pixel"/> <property name="com.jaspersoft.studio.unit.x" value="pixel"/> <property name="com.jaspersoft.studio.unit.width" value="pixel"/> </reportElement> <textElement textAlignment="Left" verticalAlignment="Middle"/> <textFieldExpression><![CDATA["f " + $V{PAGE_NUMBER}]]></textFieldExpression> </textField> <textField pattern="MM/dd/yyyy hh:mm:ss a" isBlankWhenNull="true"> <reportElement x="708" y="0" width="320" height="15" uuid="3e8eb96b-1b5e-4f34-b1ec-f5770049d23e"> <property name="com.jaspersoft.studio.unit.x" value="pixel"/> </reportElement> <box leftPadding="5"/> <textElement textAlignment="Right" verticalAlignment="Middle"> <font fontName="SansSerif" isBold="false"/> </textElement> <textFieldExpression><![CDATA[$P{runDate}]]></textFieldExpression> </textField> </band> </pageFooter> </jasperReport> 我收到以下错误:

  

变量'fuffa'在文件中有2个维度,这与索引下标中的3个维度不匹配。

如何说服MATLAB追加第三维?

1 个答案:

答案 0 :(得分:1)

因为您正在访问磁盘上的文件而不是工作区变量,所以您可能会遇到扩展维数的问题。处理存储在内存中的变量时,您不会遇到此问题(例如,如果您使用load而不是matfile)。

为避免这种情况,最好的方法是pre-allocation。我将假设这是对您实际问题的简化,并且您需要能够对可能的2D阵列进行此类3D扩展。

在这种情况下,只需使用cat在第三维中连接:

fuffa=rand(10,10);
save('fuffalo','fuffa', '-v7.3')
m1 = matfile('fuffalo.mat', 'Writable', true);
for ii=1:3
    fuffa2=rand(10,10);
    % Concatenating in the 3rd dimension, avoiding used 'end' which 
    % assumes that dimension already exists
    m1.fuffa=cat(3,m1.fuffa,fuffa2);
end
% m1.fuffa <10x10x4 double>

请注意,通过这样做,您将整个.mat数据带入内存以进行连接,从而使matfile()无效。但是,您之前的方法会遇到同样的问题,因为在docs中我们看到了:

  

使用end关键字作为索引的一部分会导致MATLAB将整个变量加载到内存中。

如前所述,预分配你可能会更好!